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

Skip to content

Commit 740bce0

Browse files
committed
memcached cache pool adapter
1 parent d6e8937 commit 740bce0

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines 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 <[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+
/**
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+
$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+
}
Lines 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 <[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+
/**
17+
* @author Rob Frawley 2nd <[email protected]>
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') || !version_compare(phpversion('memcached'), '2.1.0', '>')) {
40+
self::markTestSkipped('Extension memcached >2.1.0 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)