Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1363664

Browse files
committed
[Cache] Use options from Memcached DSN
1 parent 481e31c commit 1363664

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added PruneableInterface so PSR-6 or PSR-16 cache implementations can declare support for manual stale cache pruning
88
* added FilesystemTrait::prune() and PhpFilesTrait::prune() implementations
9+
* added Using options from Memcached DSN
910
* now FilesystemAdapter, PhpFilesAdapter, FilesystemCache, and PhpFilesCache implement PruneableInterface and support
1011
manual stale cache pruning
1112

src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,32 @@ public function provideServersSetting()
162162
);
163163
}
164164
}
165+
166+
/**
167+
* @dataProvider provideDsnWithOptions
168+
*/
169+
public function testDsnWithOptions($dsn, array $options)
170+
{
171+
$client = MemcachedAdapter::createConnection($dsn);
172+
173+
foreach ($options as $option => $expect) {
174+
$this->assertSame($expect, $client->getOption($option));
175+
}
176+
}
177+
178+
public function provideDsnWithOptions()
179+
{
180+
if (!class_exists('\Memcached')) {
181+
self::markTestSkipped('Extension memcached required.');
182+
}
183+
184+
yield array(
185+
'memcached://localhost:11222?retry_timeout=10',
186+
array(\Memcached::OPT_RETRY_TIMEOUT => 10),
187+
);
188+
yield array(
189+
'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
190+
array(\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2),
191+
);
192+
}
165193
}

src/Symfony/Component/Cache/Traits/MemcachedTrait.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,6 @@ public static function createConnection($servers, array $options = array())
8383
$client = new \Memcached($options['persistent_id']);
8484
$username = $options['username'];
8585
$password = $options['password'];
86-
unset($options['persistent_id'], $options['username'], $options['password']);
87-
$options = array_change_key_case($options, CASE_UPPER);
88-
89-
// set client's options
90-
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
91-
$client->setOption(\Memcached::OPT_NO_BLOCK, true);
92-
if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
93-
$client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
94-
}
95-
foreach ($options as $name => $value) {
96-
if (is_int($name)) {
97-
continue;
98-
}
99-
if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) {
100-
$value = constant('Memcached::'.$name.'_'.strtoupper($value));
101-
}
102-
$opt = constant('Memcached::OPT_'.$name);
103-
104-
unset($options[$name]);
105-
$options[$opt] = $value;
106-
}
107-
$client->setOptions($options);
10886

10987
// parse any DSN in $servers
11088
foreach ($servers as $i => $dsn) {
@@ -139,11 +117,34 @@ public static function createConnection($servers, array $options = array())
139117
if (isset($params['query'])) {
140118
parse_str($params['query'], $query);
141119
$params += $query;
120+
$options += $query;
142121
}
143122

144123
$servers[$i] = array($params['host'], $params['port'], $params['weight']);
145124
}
146125

126+
// set client's options
127+
unset($options['persistent_id'], $options['username'], $options['password'], $options['weight']);
128+
$options = array_change_key_case($options, CASE_UPPER);
129+
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
130+
$client->setOption(\Memcached::OPT_NO_BLOCK, true);
131+
if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
132+
$client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
133+
}
134+
foreach ($options as $name => $value) {
135+
if (is_int($name)) {
136+
continue;
137+
}
138+
if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) {
139+
$value = constant('Memcached::'.$name.'_'.strtoupper($value));
140+
}
141+
$opt = constant('Memcached::OPT_'.$name);
142+
143+
unset($options[$name]);
144+
$options[$opt] = $value;
145+
}
146+
$client->setOptions($options);
147+
147148
// set client's servers, taking care of persistent connections
148149
if (!$client->isPristine()) {
149150
$oldServers = array();

0 commit comments

Comments
 (0)