Skip to content

Commit c7f9bfc

Browse files
committed
Merge pull request #29 from cdaguerre/master
Fixed persistent connections
2 parents d92144a + 3045f21 commit c7f9bfc

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Then add parameters (probably in config.yml) for your servers, and options
3939
aequasi_cache:
4040
instances:
4141
default:
42-
persistent: true
42+
persistent: true # Boolean or persistent_id
4343
namespace: mc
4444
type: memcached
4545
hosts:
@@ -115,11 +115,11 @@ Here is an example usage of the service:
115115
$cache = $this->get( 'aequasi_cache.instance.default' );
116116
$key = 'test';
117117
if( $data = $cache->fetch( $key ) ) {
118-
print_r( $data );
118+
print_r( $data );
119119
} else {
120-
/** @var $em \Doctrine\ORM\EntityManager */
121-
$data = $em->find( 'AcmeDemoBundle:User', 1 );
122-
$cache->save( $key, $data, 3600 );
120+
/** @var $em \Doctrine\ORM\EntityManager */
121+
$data = $em->find( 'AcmeDemoBundle:User', 1 );
122+
$cache->save( $key, $data, 3600 );
123123
}
124124
```
125125

src/Cache/Memcached.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function addServer($host, $port, $weight = 0)
2222
{
2323
$serverList = $this->getServerList();
2424
foreach ($serverList as $server) {
25-
if ($server == array('host' => $host, 'port' => $port, 'weight' => $weight)) {
25+
if ($server['host'] === $host && $server['port'] === $port) {
2626
return false;
2727
}
2828
}

src/DependencyInjection/Builder/ServiceBuilder.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,35 @@ public function createCacheInstance(Definition $service, $type, $id, array $inst
157157
if (empty($instance['id'])) {
158158
$cache = new Definition(self::$types[$type]['class']);
159159

160-
if (isset($instance['persistent']) && $instance['persistent'] === true) {
160+
if (isset($instance['persistent']) && $instance['persistent'] !== false) {
161+
if ($instance['persistent'] !== true) {
162+
$persistentId = $instance['persistent'];
163+
} else {
164+
$persistentId = substr(md5(serialize($instance['hosts'])), 0, 5);
165+
}
161166
if ($type === 'memcached') {
162-
$cache->setArguments(array(serialize($instance['hosts'])));
167+
$cache->setArguments(array($persistentId));
163168
}
164169
if ($type === 'redis') {
165170
self::$types[$type]['connect'] = 'pconnect';
166171
}
167172
}
168173

169174
foreach ($instance['hosts'] as $config) {
170-
$host = empty($config['host']) ? 'localhost' : $config['host'];
171-
$port = empty($config['port']) ? 11211 : $config['port'];
175+
$arguments = array(
176+
'host' => empty($config['host']) ? 'localhost' : $config['host'],
177+
'port' => empty($config['port']) ? 11211 : $config['port']
178+
);
172179
if ($type === 'memcached') {
173-
$thirdParam = is_null($config['weight']) ? 0 : $config['weight'];
180+
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
174181
} else {
175-
$thirdParam = is_null($config['timeout']) ? 0 : $config['timeout'];
182+
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
183+
if (isset($persistentId)) {
184+
$arguments[] = $persistentId;
185+
}
176186
}
177187

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

src/DependencyInjection/Configuration.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@ private function getClustersNode()
8383
->defaultNull()
8484
->info("For Redis: Specify what database you want.")
8585
->end()
86-
->booleanNode('persistent')
86+
->scalarNode('persistent')
8787
->defaultNull()
88-
->info("For Redis and Memcached: Specify if you want persistent connections.")
88+
->beforeNormalization()
89+
->ifTrue(
90+
function($v) {
91+
return $v === 'true' || $v === 'false';
92+
}
93+
)
94+
->then(
95+
function($v) {
96+
return (bool) $v;
97+
}
98+
)
99+
->end()
100+
->info("For Redis and Memcached: Specify the persistent id if you want persistent connections.")
89101
->end()
90102
->scalarNode('auth_password')
91103
->info("For Redis: Authorization info.")

0 commit comments

Comments
 (0)