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 b79c716

Browse filesBrowse files
committed
feature #20858 [Cache] Simple Memcached Adapter (robfrawley)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Cache] Simple Memcached Adapter | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | | Related PRs | #20863, ~~#20752~~ Commits ------- 12de2ae memcached cache pool adapter
2 parents 38df506 + 12de2ae commit b79c716
Copy full SHA for b79c716

File tree

Expand file treeCollapse file tree

2 files changed

+130
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+130
-0
lines changed
+80Lines changed: 80 additions & 0 deletions
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+
}
+50Lines 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 <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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.