Skip to content

Commit

Permalink
Merge pull request #7 from Nyholm/hierarchy
Browse files Browse the repository at this point in the history
Added support for HierarchicalPoolInterface
  • Loading branch information
cryptiklemur committed Jan 13, 2016
2 parents 996cdc8 + 2673ec2 commit 685889f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 60 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
],
"require":
{
"php": "^5.5|^7.0",
"psr/cache": "1.0.0",
"cache/adapter-common": "^0.1.2",
"cache/taggable-cache": "^0.2",
"predis/predis": "^1.0"
"php": "^5.5|^7.0",
"psr/cache": "1.0.0",
"cache/adapter-common": "^0.1.2",
"cache/taggable-cache": "^0.3",
"cache/hierarchical-cache": "^0.1",
"predis/predis": "^1.0"
},
"require-dev":
{
Expand Down
39 changes: 20 additions & 19 deletions src/PredisCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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()
Expand All @@ -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);
}
}
34 changes: 34 additions & 0 deletions tests/CreatePoolTrait.php
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;
}
}
19 changes: 19 additions & 0 deletions tests/IntegrationHierarchyTest.php
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;
}
22 changes: 3 additions & 19 deletions tests/IntegrationPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,9 @@

namespace Cache\Adapter\Predis\Tests;

use Cache\Adapter\Predis\PredisCachePool;
use Cache\IntegrationTests\CachePoolTest as BaseTest;
use Predis\Client;
use Cache\IntegrationTests\CachePoolTest;

class IntegrationPoolTest extends BaseTest
class IntegrationPoolTest extends CachePoolTest
{
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;
}
use CreatePoolTrait;
}
18 changes: 1 addition & 17 deletions tests/IntegrationTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,9 @@

namespace Cache\Adapter\Predis\Tests;

use Cache\Adapter\Predis\PredisCachePool;
use Cache\IntegrationTests\TaggableCachePoolTest;
use Predis\Client;

class IntegrationTagTest extends TaggableCachePoolTest
{
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;
}
use CreatePoolTrait;
}

0 comments on commit 685889f

Please sign in to comment.