Skip to content

[Cache] Use options from Memcached DSN #23978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 FilesystemTrait::prune() and PhpFilesTrait::prune() implementations
* now FilesystemAdapter, PhpFilesAdapter, FilesystemCache, and PhpFilesCache implement PruneableInterface and support
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,34 @@ public function provideServersSetting()
);
}
}

/**
* @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),
);
}
}
45 changes: 23 additions & 22 deletions src/Symfony/Component/Cache/Traits/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ public static function createConnection($servers, array $options = array())
$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) {
Expand Down Expand Up @@ -139,11 +117,34 @@ public static function createConnection($servers, array $options = array())
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();
Expand Down