Skip to content

Commit 12de2ae

Browse files
committed
memcached cache pool adapter
1 parent d6e8937 commit 12de2ae

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 static function isSupported()
22+
{
23+
return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>=');
24+
}
25+
26+
public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0)
27+
{
28+
parent::__construct($namespace, $defaultLifetime);
29+
$this->client = $client;
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 ($this->client->deleteMulti($ids) as $result) {
65+
if (\Memcached::RES_SUCCESS === $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+
return $this->client->flush();
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
46+
public function testIsSupported()
47+
{
48+
$this->assertTrue(MemcachedAdapter::isSupported());
49+
}
50+
}

0 commit comments

Comments
 (0)