-
Notifications
You must be signed in to change notification settings - Fork 16
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 #7 from Nyholm/hierarchy
Added support for HierarchicalPoolInterface
- Loading branch information
Showing
6 changed files
with
83 additions
and
60 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,15 +12,18 @@ | |
namespace Cache\Adapter\Predis; | ||
|
||
use Cache\Adapter\Common\AbstractCachePool; | ||
use Cache\Hierarchy\HierarchicalCachePoolTrait; | ||
use Cache\Hierarchy\HierarchicalPoolInterface; | ||
use Predis\Client; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* @author Aaron Scherer <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class PredisCachePool extends AbstractCachePool | ||
class PredisCachePool extends AbstractCachePool implements HierarchicalPoolInterface | ||
{ | ||
use HierarchicalCachePoolTrait; | ||
|
||
/** | ||
* @type Client | ||
*/ | ||
|
@@ -34,24 +37,9 @@ public function __construct(Client $cache) | |
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem($key, array $tags = []) | ||
{ | ||
$this->validateKey($key); | ||
$taggedKey = $this->generateCacheKey($key, $tags); | ||
|
||
if (isset($this->deferred[$key])) { | ||
return true; | ||
} | ||
|
||
return $this->cache->exists($taggedKey); | ||
} | ||
|
||
protected function fetchObjectFromCache($key) | ||
{ | ||
return unserialize($this->cache->get($key)); | ||
return unserialize($this->cache->get($this->getHierarchyKey($key))); | ||
} | ||
|
||
protected function clearAllObjectsFromCache() | ||
|
@@ -61,15 +49,28 @@ protected function clearAllObjectsFromCache() | |
|
||
protected function clearOneObjectFromCache($key) | ||
{ | ||
return $this->cache->del($key) >= 0; | ||
// We have to commit here to be able to remove deferred hierarchy items | ||
$this->commit(); | ||
|
||
$keyString = $this->getHierarchyKey($key, $path); | ||
$this->cache->incr($path); | ||
$this->clearHierarchyKeyCache(); | ||
|
||
return $this->cache->del($keyString) >= 0; | ||
} | ||
|
||
protected function storeItemInCache($key, CacheItemInterface $item, $ttl) | ||
{ | ||
$key = $this->getHierarchyKey($key); | ||
if ($ttl === null) { | ||
return 'OK' === $this->cache->set($key, serialize($item))->getPayload(); | ||
} | ||
|
||
return 'OK' === $this->cache->setex($key, $ttl, serialize($item))->getPayload(); | ||
} | ||
|
||
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\predis-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\Predis\Tests; | ||
|
||
use Cache\Adapter\Predis\PredisCachePool; | ||
use Predis\Client; | ||
|
||
trait CreatePoolTrait | ||
{ | ||
private $client = null; | ||
|
||
public function createCachePool() | ||
{ | ||
return new PredisCachePool($this->getClient()); | ||
} | ||
|
||
private function getClient() | ||
{ | ||
if ($this->client === null) { | ||
$this->client = new Client('tcp:/127.0.0.1:6379'); | ||
} | ||
|
||
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\predis-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\Predis\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