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
-------

5798dd6bec [Cache] Use options from Memcached DSN
This commit is contained in:
Fabien Potencier 2017-09-05 11:44:31 -07:00
commit c0b4e564db
3 changed files with 54 additions and 22 deletions

View File

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

View File

@ -161,4 +161,34 @@ class MemcachedAdapterTest extends AdapterTestCase
);
}
}
/**
* @dataProvider provideDsnWithOptions
*/
public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
{
$client = MemcachedAdapter::createConnection($dsn, $options);
foreach ($expectedOptions as $option => $expect) {
$this->assertSame($expect, $client->getOption($option));
}
}
public function provideDsnWithOptions()
{
if (!class_exists('\Memcached')) {
self::markTestSkipped('Extension memcached required.');
}
yield array(
'memcached://localhost:11222?retry_timeout=10',
array(\Memcached::OPT_RETRY_TIMEOUT => 8),
array(\Memcached::OPT_RETRY_TIMEOUT => 10),
);
yield array(
'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
array(\Memcached::OPT_RETRY_TIMEOUT => 8),
array(\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8),
);
}
}

View File

@ -89,28 +89,6 @@ trait MemcachedTrait
$client = new \Memcached($options['persistent_id']);
$username = $options['username'];
$password = $options['password'];
unset($options['persistent_id'], $options['username'], $options['password']);
$options = array_change_key_case($options, CASE_UPPER);
// set client's options
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$client->setOption(\Memcached::OPT_NO_BLOCK, true);
if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
$client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
}
foreach ($options as $name => $value) {
if (is_int($name)) {
continue;
}
if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) {
$value = constant('Memcached::'.$name.'_'.strtoupper($value));
}
$opt = constant('Memcached::OPT_'.$name);
unset($options[$name]);
$options[$opt] = $value;
}
$client->setOptions($options);
// parse any DSN in $servers
foreach ($servers as $i => $dsn) {
@ -145,11 +123,34 @@ trait MemcachedTrait
if (isset($params['query'])) {
parse_str($params['query'], $query);
$params += $query;
$options = $query + $options;
}
$servers[$i] = array($params['host'], $params['port'], $params['weight']);
}
// set client's options
unset($options['persistent_id'], $options['username'], $options['password'], $options['weight']);
$options = array_change_key_case($options, CASE_UPPER);
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$client->setOption(\Memcached::OPT_NO_BLOCK, true);
if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
$client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
}
foreach ($options as $name => $value) {
if (is_int($name)) {
continue;
}
if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) {
$value = constant('Memcached::'.$name.'_'.strtoupper($value));
}
$opt = constant('Memcached::OPT_'.$name);
unset($options[$name]);
$options[$opt] = $value;
}
$client->setOptions($options);
// set client's servers, taking care of persistent connections
if (!$client->isPristine()) {
$oldServers = array();