-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Nyholm/updates
Updates to adapter-common 0.2
- Loading branch information
Showing
6 changed files
with
85 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,18 @@ | |
namespace Cache\Adapter\Memcached; | ||
|
||
use Cache\Adapter\Common\AbstractCachePool; | ||
use Cache\Hierarchy\HierarchicalCachePoolTrait; | ||
use Cache\Hierarchy\HierarchicalPoolInterface; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* @author Aaron Scherer <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class MemcachedCachePool extends AbstractCachePool | ||
class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface | ||
{ | ||
use HierarchicalCachePoolTrait; | ||
|
||
/** | ||
* @type \Memcached | ||
*/ | ||
|
@@ -31,11 +35,16 @@ class MemcachedCachePool extends AbstractCachePool | |
public function __construct(\Memcached $cache) | ||
{ | ||
$this->cache = $cache; | ||
$this->cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); | ||
} | ||
|
||
protected function fetchObjectFromCache($key) | ||
{ | ||
return $this->cache->get($key); | ||
if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) { | ||
return [false, null]; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected function clearAllObjectsFromCache() | ||
|
@@ -45,6 +54,11 @@ protected function clearAllObjectsFromCache() | |
|
||
protected function clearOneObjectFromCache($key) | ||
{ | ||
$this->commit(); | ||
$key = $this->getHierarchyKey($key, $path); | ||
$this->cache->increment($path, 1, 0); | ||
$this->clearHierarchyKeyCache(); | ||
|
||
if ($this->cache->delete($key)) { | ||
return true; | ||
} | ||
|
@@ -59,6 +73,13 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl) | |
$ttl = 0; | ||
} | ||
|
||
return $this->cache->set($key, $item, $ttl); | ||
$key = $this->getHierarchyKey($key); | ||
|
||
return $this->cache->set($key, serialize([true, $item->get()]), $ttl); | ||
} | ||
|
||
protected function getValueFormStore($key) | ||
{ | ||
return $this->cache->get($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\memcached-adapter package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\Adapter\Memcached\Tests; | ||
|
||
use Cache\Adapter\Memcached\MemcachedCachePool; | ||
|
||
trait CreatePoolTrait | ||
{ | ||
private $client = null; | ||
|
||
public function createCachePool() | ||
{ | ||
return new MemcachedCachePool($this->getClient()); | ||
} | ||
|
||
private function getClient() | ||
{ | ||
if ($this->client === null) { | ||
$this->client = new \Memcached(); | ||
$this->client->addServer('localhost', 11211); | ||
} | ||
|
||
return $this->client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\memcached-adapter package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\Adapter\Memcached\Tests; | ||
|
||
use Cache\IntegrationTests\HierarchicalCachePoolTest; | ||
|
||
class IntegrationHierarchyTest extends HierarchicalCachePoolTest | ||
{ | ||
use CreatePoolTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters