Skip to content

Commit

Permalink
Merge pull request #2 from php-cache/hierarchy
Browse files Browse the repository at this point in the history
Added a trait to use with adapters
  • Loading branch information
cryptiklemur committed Jan 12, 2016
2 parents dbdb135 + d37dbb7 commit 186e719
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 295 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cache/hierarchical-cache",
"description": "A PSR-6 cache implementation using hierarchical. This implementation supports tags",
"description": "A helper trait and interface to your PSR-6 cache to support hierarchical keys.",
"type": "library",
"license": "MIT",
"minimum-stability": "stable",
Expand Down Expand Up @@ -30,7 +30,7 @@
"php": "^5.5|^7.0",
"psr/cache": "1.0.0",
"cache/adapter-common": "^0.1",
"cache/taggable-cache": "^0.2"
"cache/taggable-cache": "^0.3"
},
"require-dev":
{
Expand Down
201 changes: 0 additions & 201 deletions src/HierarchicalCachePool.php

This file was deleted.

124 changes: 124 additions & 0 deletions src/HierarchicalCachePoolTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/*
* This file is part of php-cache\hierarchical-cache 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\Hierarchy;

use Cache\Taggable\TaggablePoolInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
trait HierarchicalCachePoolTrait
{
/**
* A temporary cache for keys.
*
* @type array
*/
private $keyCache = [];

/**
* Get a value form the store. This must not be an PoolItemInterface.
*
* @param string $key
*
* @return string|null
*/
abstract protected function getValueFormStore($key);

/**
* Get a key to use with the hierarchy. If the key does not start with HierarchicalPoolInterface::SEPARATOR
* this will return an unalterered key. This function supports a tagged key. Ie "foo:bar".
*
* @param string $key The original key
* @param string &$pathKey A cache key for the path. If this key is changed everything beyond that path is changed.
*
* @return string
*/
protected function getHierarchyKey($key, &$pathKey = null)
{
if (!$this->isHierarchyKey($key)) {
return $key;
}

$key = $this->explodeKey($key);

$keyString = '';
// The comments below is for a $key = ["foo!tagHash", "bar!tagHash"]
foreach ($key as $name) {
// 1) $keyString = "foo!tagHash"
// 2) $keyString = "foo!tagHash![foo_index]!bar!tagHash"
$keyString .= $name;
$pathKey = 'path'.TaggablePoolInterface::TAG_SEPARATOR.$keyString;

if (isset($this->keyCache[$pathKey])) {
$index = $this->keyCache[$pathKey];
} else {
$index = $this->getValueFormStore($pathKey);
$this->keyCache[$pathKey] = $index;
}

// 1) $keyString = "foo!tagHash![foo_index]!"
// 2) $keyString = "foo!tagHash![foo_index]!bar!tagHash![bar_index]!"
$keyString .= TaggablePoolInterface::TAG_SEPARATOR.$index.TaggablePoolInterface::TAG_SEPARATOR;
}

// Assert: $pathKey = "path!foo!tagHash![foo_index]!bar!tagHash"
// Assert: $keyString = "foo!tagHash![foo_index]!bar!tagHash![bar_index]!"

return $keyString;
}

/**
* Clear the cache for the keys.
*/
protected function clearHierarchyKeyCache()
{
$this->keyCache = [];
}

/**
* A hierarchy key MUST begin with the separator.
*
* @param string $key
*
* @return bool
*/
private function isHierarchyKey($key)
{
return substr($key, 0, 1) === HierarchicalPoolInterface::HIERARCHY_SEPARATOR;
}

/**
* This will take a hierarchy key ("|foo|bar") with tags ("|foo|bar!tagHash") and return an array with
* each level in the hierarchy appended with the tags. ["foo!tagHash", "bar!tagHash"].
*
* @param string $key
*
* @return array
*/
private function explodeKey($string)
{
list($key, $tag) = explode(TaggablePoolInterface::TAG_SEPARATOR, $string.TaggablePoolInterface::TAG_SEPARATOR);

if ($key === HierarchicalPoolInterface::HIERARCHY_SEPARATOR) {
$parts = ['root'];
} else {
$parts = explode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $key);
// remove first element since it is always empty and replace it with 'root'
$parts[0] = 'root';
}

return array_map(function ($level) use ($tag) {
return $level.TaggablePoolInterface::TAG_SEPARATOR.$tag;
}, $parts);
}
}
10 changes: 10 additions & 0 deletions src/HierarchicalPoolInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?php

/*
* This file is part of php-cache\hierarchical-cache 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\Hierarchy;

interface HierarchicalPoolInterface
{
const HIERARCHY_SEPARATOR = '|';
}
Loading

0 comments on commit 186e719

Please sign in to comment.