Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5ed119

Browse filesBrowse files
committed
memcached cache pool adapter
1 parent d6e8937 commit c5ed119
Copy full SHA for c5ed119

File tree

2 files changed

+169
-0
lines changed
Filter options

2 files changed

+169
-0
lines changed
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Adapter;
13+
14+
/**
15+
* @author Rob Frawley 2nd <rmf@src.run>
16+
*/
17+
class MemcachedAdapter extends AbstractAdapter
18+
{
19+
private $client;
20+
21+
/**
22+
* @param \Memcached $client
23+
*/
24+
public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0)
25+
{
26+
parent::__construct($namespace, $defaultLifetime);
27+
$this->client = $client;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function doSave(array $values, $lifetime)
34+
{
35+
return $this->client->setMulti($values, $lifetime)
36+
&& $this->client->getResultCode() === \Memcached::RES_SUCCESS;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function doFetch(array $ids)
43+
{
44+
foreach ($this->client->getMulti($ids) as $id => $val) {
45+
yield $id => $val;
46+
}
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function doHave($id)
53+
{
54+
return $this->client->get($id) !== false
55+
&& $this->client->getResultCode() !== \Memcached::RES_NOTFOUND;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
protected function doDelete(array $ids)
62+
{
63+
$toDelete = count($ids);
64+
foreach ((array) $this->client->deleteMulti($ids) as $result) {
65+
if (true === $result || \Memcached::RES_NOTFOUND === $result) {
66+
--$toDelete;
67+
}
68+
}
69+
70+
return 0 === $toDelete;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
protected function doClear($namespace)
77+
{
78+
if (!isset($namespace[0]) || false === $ids = $this->getIdsByPrefix($namespace)) {
79+
return $this->client->flush();
80+
}
81+
82+
$return = true;
83+
84+
do {
85+
$return = $this->doDelete($ids) && $return;
86+
} while ($ids = $this->getIdsByPrefix($namespace));
87+
88+
return $return;
89+
}
90+
91+
private function getIdsByPrefix($namespace)
92+
{
93+
if (false === $ids = $this->client->getAllKeys()) {
94+
return false;
95+
}
96+
97+
return array_filter((array) $ids, function ($id) use ($namespace) {
98+
return 0 === strpos($id, $namespace);
99+
});
100+
}
101+
}
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
15+
16+
/**
17+
* @author Rob Frawley 2nd <rmf@src.run>
18+
*/
19+
class MemcachedAdapterTest extends AdapterTestCase
20+
{
21+
protected $skippedTests = array(
22+
//'testExpiration' => 'Testing expiration slows down the test suite',
23+
//'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
24+
//'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
25+
);
26+
27+
private static $client;
28+
29+
private static function clientDefaultServer()
30+
{
31+
return array(
32+
getenv('MEMCACHED_HOST') ?: '127.0.0.1',
33+
getenv('MEMCACHED_PORT') ?: 11211,
34+
);
35+
}
36+
37+
public static function setupBeforeClass()
38+
{
39+
if (!extension_loaded('memcached')) {
40+
self::markTestSkipped('Extension memcached required.');
41+
}
42+
43+
self::$client = new \Memcached();
44+
self::$client->addServers(array(static::clientDefaultServer()));
45+
46+
parent::setupBeforeClass();
47+
}
48+
49+
public function createCachePool($defaultLifetime = 0)
50+
{
51+
return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
52+
}
53+
54+
/**
55+
* @group memcachedAdapter
56+
*/
57+
public function testAdapterAndClientCreationAndServers()
58+
{
59+
$this->assertInstanceOf(MemcachedAdapter::class, static::createCachePool(),
60+
'Adapter created should be instance of MemcachedAdapter.');
61+
62+
$this->assertInstanceOf(\Memcached::class, static::$client,
63+
'Client created should be instance of Memcached.');
64+
65+
$this->assertCount(1, static::$client->getServerList(),
66+
'No registered servers should exist with Memcached client.');
67+
}
68+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.