Skip to content

Commit

Permalink
Merge pull request #29 from cdaguerre/master
Browse files Browse the repository at this point in the history
Fixed persistent connections
  • Loading branch information
cryptiklemur committed Apr 22, 2014
2 parents d92144a + 3045f21 commit c7f9bfc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Then add parameters (probably in config.yml) for your servers, and options
aequasi_cache:
instances:
default:
persistent: true
persistent: true # Boolean or persistent_id
namespace: mc
type: memcached
hosts:
Expand Down Expand Up @@ -115,11 +115,11 @@ Here is an example usage of the service:
$cache = $this->get( 'aequasi_cache.instance.default' );
$key = 'test';
if( $data = $cache->fetch( $key ) ) {
print_r( $data );
print_r( $data );
} else {
/** @var $em \Doctrine\ORM\EntityManager */
$data = $em->find( 'AcmeDemoBundle:User', 1 );
$cache->save( $key, $data, 3600 );
/** @var $em \Doctrine\ORM\EntityManager */
$data = $em->find( 'AcmeDemoBundle:User', 1 );
$cache->save( $key, $data, 3600 );
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function addServer($host, $port, $weight = 0)
{
$serverList = $this->getServerList();
foreach ($serverList as $server) {
if ($server == array('host' => $host, 'port' => $port, 'weight' => $weight)) {
if ($server['host'] === $host && $server['port'] === $port) {
return false;
}
}
Expand Down
24 changes: 17 additions & 7 deletions src/DependencyInjection/Builder/ServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,35 @@ public function createCacheInstance(Definition $service, $type, $id, array $inst
if (empty($instance['id'])) {
$cache = new Definition(self::$types[$type]['class']);

if (isset($instance['persistent']) && $instance['persistent'] === true) {
if (isset($instance['persistent']) && $instance['persistent'] !== false) {
if ($instance['persistent'] !== true) {
$persistentId = $instance['persistent'];
} else {
$persistentId = substr(md5(serialize($instance['hosts'])), 0, 5);
}
if ($type === 'memcached') {
$cache->setArguments(array(serialize($instance['hosts'])));
$cache->setArguments(array($persistentId));
}
if ($type === 'redis') {
self::$types[$type]['connect'] = 'pconnect';
}
}

foreach ($instance['hosts'] as $config) {
$host = empty($config['host']) ? 'localhost' : $config['host'];
$port = empty($config['port']) ? 11211 : $config['port'];
$arguments = array(
'host' => empty($config['host']) ? 'localhost' : $config['host'],
'port' => empty($config['port']) ? 11211 : $config['port']
);
if ($type === 'memcached') {
$thirdParam = is_null($config['weight']) ? 0 : $config['weight'];
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
} else {
$thirdParam = is_null($config['timeout']) ? 0 : $config['timeout'];
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
if (isset($persistentId)) {
$arguments[] = $persistentId;
}
}

$cache->addMethodCall(self::$types[$type]['connect'], array($host, $port, $thirdParam));
$cache->addMethodCall(self::$types[$type]['connect'], $arguments);
}
unset($config);

Expand Down
16 changes: 14 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,21 @@ private function getClustersNode()
->defaultNull()
->info("For Redis: Specify what database you want.")
->end()
->booleanNode('persistent')
->scalarNode('persistent')
->defaultNull()
->info("For Redis and Memcached: Specify if you want persistent connections.")
->beforeNormalization()
->ifTrue(
function($v) {
return $v === 'true' || $v === 'false';
}
)
->then(
function($v) {
return (bool) $v;
}
)
->end()
->info("For Redis and Memcached: Specify the persistent id if you want persistent connections.")
->end()
->scalarNode('auth_password')
->info("For Redis: Authorization info.")
Expand Down

0 comments on commit c7f9bfc

Please sign in to comment.