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

Skip to content

Commit c0b4e56

Browse files
committed
feature #23978 [Cache] Use options from Memcached DSN (Bukashk0zzz)
This PR was merged into the 3.4 branch. Discussion ---------- [Cache] Use options from Memcached DSN | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23962 | License | MIT | Doc PR | - This PR allows to pass options in cache config. Example: ```yaml framework: cache: app: cache.adapter.memcached default_memcached_provider: 'memcached://%env(MEMCACHE_HOST)%?socket_recv_size=1&socket_send_size=2' ``` Commits ------- 5798dd6 [Cache] Use options from Memcached DSN
2 parents 4f02e91 + 5798dd6 commit c0b4e56

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

src/Symfony/Component/Cache/CHANGELOG.md

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

7+
* added using options from Memcached DSN
78
* added PruneableInterface so PSR-6 or PSR-16 cache implementations can declare support for manual stale cache pruning
89
* added prune logic to FilesystemTrait, PhpFilesTrait, PdoTrait, TagAwareAdapter and ChainTrait
910
* now FilesystemAdapter, PhpFilesAdapter, FilesystemCache, PhpFilesCache, PdoAdapter, PdoCache, ChainAdapter, and

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,34 @@ public function provideServersSetting()
161161
);
162162
}
163163
}
164+
165+
/**
166+
* @dataProvider provideDsnWithOptions
167+
*/
168+
public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
169+
{
170+
$client = MemcachedAdapter::createConnection($dsn, $options);
171+
172+
foreach ($expectedOptions as $option => $expect) {
173+
$this->assertSame($expect, $client->getOption($option));
174+
}
175+
}
176+
177+
public function provideDsnWithOptions()
178+
{
179+
if (!class_exists('\Memcached')) {
180+
self::markTestSkipped('Extension memcached required.');
181+
}
182+
183+
yield array(
184+
'memcached://localhost:11222?retry_timeout=10',
185+
array(\Memcached::OPT_RETRY_TIMEOUT => 8),
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_RETRY_TIMEOUT => 8),
191+
array(\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8),
192+
);
193+
}
164194
}

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

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

11593
// parse any DSN in $servers
11694
foreach ($servers as $i => $dsn) {
@@ -145,11 +123,34 @@ public static function createConnection($servers, array $options = array())
145123
if (isset($params['query'])) {
146124
parse_str($params['query'], $query);
147125
$params += $query;
126+
$options = $query + $options;
148127
}
149128

150129
$servers[$i] = array($params['host'], $params['port'], $params['weight']);
151130
}
152131

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

0 commit comments

Comments
 (0)