diff --git a/README.md b/README.md index bce9dfc..2b9add8 100755 --- a/README.md +++ b/README.md @@ -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: @@ -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 ); } ``` diff --git a/src/Cache/Memcached.php b/src/Cache/Memcached.php index cf84f6b..be8a42f 100644 --- a/src/Cache/Memcached.php +++ b/src/Cache/Memcached.php @@ -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; } } diff --git a/src/DependencyInjection/Builder/ServiceBuilder.php b/src/DependencyInjection/Builder/ServiceBuilder.php index eb5335d..bb993eb 100755 --- a/src/DependencyInjection/Builder/ServiceBuilder.php +++ b/src/DependencyInjection/Builder/ServiceBuilder.php @@ -157,9 +157,14 @@ 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'; @@ -167,15 +172,20 @@ public function createCacheInstance(Definition $service, $type, $id, array $inst } 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); diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 50a7b7e..170dff0 100755 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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.")