Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1143944

Browse files
committed
memcached cache pool adapter
1 parent d6e8937 commit 1143944

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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+
public static function isSupported()
31+
{
32+
return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>=');
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function doSave(array $values, $lifetime)
39+
{
40+
return $this->client->setMulti($values, $lifetime)
41+
&& $this->client->getResultCode() === \Memcached::RES_SUCCESS;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
protected function doFetch(array $ids)
48+
{
49+
return $this->client->getMulti($ids);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function doHave($id)
56+
{
57+
return $this->client->get($id) !== false
58+
|| $this->client->getResultCode() !== \Memcached::RES_NOTFOUND;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
protected function doDelete(array $ids)
65+
{
66+
$toDelete = count($ids);
67+
foreach ((array) $this->client->deleteMulti($ids) as $result) {
68+
if (true === $result || \Memcached::RES_NOTFOUND === $result) {
69+
--$toDelete;
70+
}
71+
}
72+
73+
return 0 === $toDelete;
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
protected function doClear($namespace)
80+
{
81+
if (!isset($namespace[0]) || false === $ids = $this->getIdsByPrefix($namespace)) {
82+
return $this->client->flush();
83+
}
84+
85+
$isCleared = true;
86+
do {
87+
$isCleared = $this->doDelete($ids) && $isCleared;
88+
} while ($ids = $this->getIdsByPrefix($namespace));
89+
90+
return $isCleared;
91+
}
92+
93+
private function getIdsByPrefix($namespace)
94+
{
95+
if (false === $ids = $this->client->getAllKeys()) {
96+
return false;
97+
}
98+
99+
return array_filter((array) $ids, function ($id) use ($namespace) {
100+
return 0 === strpos($id, $namespace);
101+
});
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
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 <[email protected]>
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+
private static function clientDefaultServer()
27+
{
28+
return array(
29+
getenv('MEMCACHED_HOST') ?: '127.0.0.1',
30+
getenv('MEMCACHED_PORT') ?: 11211,
31+
);
32+
}
33+
34+
public static function setupBeforeClass()
35+
{
36+
if (!MemcachedAdapter::isSupported()) {
37+
self::markTestSkipped('Extension memcached >=2.2.0 required.');
38+
}
39+
40+
self::$client = new \Memcached();
41+
self::$client->addServers(array(static::clientDefaultServer()));
42+
43+
parent::setupBeforeClass();
44+
}
45+
46+
public function createCachePool($defaultLifetime = 0)
47+
{
48+
return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
49+
}
50+
}

0 commit comments

Comments
 (0)