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

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

File tree

2 files changed

+145
-0
lines changed
Filter options

2 files changed

+145
-0
lines changed
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0)
22+
{
23+
parent::__construct($namespace, $defaultLifetime);
24+
$this->client = $client;
25+
}
26+
27+
public static function isSupported()
28+
{
29+
return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>=');
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function doSave(array $values, $lifetime)
36+
{
37+
return $this->client->setMulti($values, $lifetime)
38+
&& $this->client->getResultCode() === \Memcached::RES_SUCCESS;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function doFetch(array $ids)
45+
{
46+
return $this->client->getMulti($ids);
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_SUCCESS;
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+
$isCleared = true;
83+
do {
84+
$isCleared = $this->doDelete($ids) && $isCleared;
85+
} while ($ids = $this->getIdsByPrefix($namespace));
86+
87+
return $isCleared;
88+
}
89+
90+
private function getIdsByPrefix($namespace)
91+
{
92+
if (false === $ids = $this->client->getAllKeys()) {
93+
return false;
94+
}
95+
96+
return array_filter((array) $ids, function ($id) use ($namespace) {
97+
return 0 === strpos($id, $namespace);
98+
});
99+
}
100+
}
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
class MemcachedAdapterTest extends AdapterTestCase
17+
{
18+
protected $skippedTests = array(
19+
'testExpiration' => 'Testing expiration slows down the test suite',
20+
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
21+
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
22+
);
23+
24+
private static $client;
25+
26+
public static function setupBeforeClass()
27+
{
28+
if (!MemcachedAdapter::isSupported()) {
29+
self::markTestSkipped('Extension memcached >=2.2.0 required.');
30+
}
31+
32+
self::$client = new \Memcached();
33+
self::$client->addServers(array(array(
34+
getenv('MEMCACHED_HOST') ?: '127.0.0.1',
35+
getenv('MEMCACHED_PORT') ?: 11211,
36+
)));
37+
38+
parent::setupBeforeClass();
39+
}
40+
41+
public function createCachePool($defaultLifetime = 0)
42+
{
43+
return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
44+
}
45+
}

0 commit comments

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