Skip to content

Commit 186e719

Browse files
committed
Merge pull request #2 from php-cache/hierarchy
Added a trait to use with adapters
2 parents dbdb135 + d37dbb7 commit 186e719

8 files changed

+316
-295
lines changed

Diff for: composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cache/hierarchical-cache",
3-
"description": "A PSR-6 cache implementation using hierarchical. This implementation supports tags",
3+
"description": "A helper trait and interface to your PSR-6 cache to support hierarchical keys.",
44
"type": "library",
55
"license": "MIT",
66
"minimum-stability": "stable",
@@ -30,7 +30,7 @@
3030
"php": "^5.5|^7.0",
3131
"psr/cache": "1.0.0",
3232
"cache/adapter-common": "^0.1",
33-
"cache/taggable-cache": "^0.2"
33+
"cache/taggable-cache": "^0.3"
3434
},
3535
"require-dev":
3636
{

Diff for: src/HierarchicalCachePool.php

-201
This file was deleted.

Diff for: src/HierarchicalCachePoolTrait.php

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\hierarchical-cache package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Hierarchy;
13+
14+
use Cache\Taggable\TaggablePoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
trait HierarchicalCachePoolTrait
20+
{
21+
/**
22+
* A temporary cache for keys.
23+
*
24+
* @type array
25+
*/
26+
private $keyCache = [];
27+
28+
/**
29+
* Get a value form the store. This must not be an PoolItemInterface.
30+
*
31+
* @param string $key
32+
*
33+
* @return string|null
34+
*/
35+
abstract protected function getValueFormStore($key);
36+
37+
/**
38+
* Get a key to use with the hierarchy. If the key does not start with HierarchicalPoolInterface::SEPARATOR
39+
* this will return an unalterered key. This function supports a tagged key. Ie "foo:bar".
40+
*
41+
* @param string $key The original key
42+
* @param string &$pathKey A cache key for the path. If this key is changed everything beyond that path is changed.
43+
*
44+
* @return string
45+
*/
46+
protected function getHierarchyKey($key, &$pathKey = null)
47+
{
48+
if (!$this->isHierarchyKey($key)) {
49+
return $key;
50+
}
51+
52+
$key = $this->explodeKey($key);
53+
54+
$keyString = '';
55+
// The comments below is for a $key = ["foo!tagHash", "bar!tagHash"]
56+
foreach ($key as $name) {
57+
// 1) $keyString = "foo!tagHash"
58+
// 2) $keyString = "foo!tagHash![foo_index]!bar!tagHash"
59+
$keyString .= $name;
60+
$pathKey = 'path'.TaggablePoolInterface::TAG_SEPARATOR.$keyString;
61+
62+
if (isset($this->keyCache[$pathKey])) {
63+
$index = $this->keyCache[$pathKey];
64+
} else {
65+
$index = $this->getValueFormStore($pathKey);
66+
$this->keyCache[$pathKey] = $index;
67+
}
68+
69+
// 1) $keyString = "foo!tagHash![foo_index]!"
70+
// 2) $keyString = "foo!tagHash![foo_index]!bar!tagHash![bar_index]!"
71+
$keyString .= TaggablePoolInterface::TAG_SEPARATOR.$index.TaggablePoolInterface::TAG_SEPARATOR;
72+
}
73+
74+
// Assert: $pathKey = "path!foo!tagHash![foo_index]!bar!tagHash"
75+
// Assert: $keyString = "foo!tagHash![foo_index]!bar!tagHash![bar_index]!"
76+
77+
return $keyString;
78+
}
79+
80+
/**
81+
* Clear the cache for the keys.
82+
*/
83+
protected function clearHierarchyKeyCache()
84+
{
85+
$this->keyCache = [];
86+
}
87+
88+
/**
89+
* A hierarchy key MUST begin with the separator.
90+
*
91+
* @param string $key
92+
*
93+
* @return bool
94+
*/
95+
private function isHierarchyKey($key)
96+
{
97+
return substr($key, 0, 1) === HierarchicalPoolInterface::HIERARCHY_SEPARATOR;
98+
}
99+
100+
/**
101+
* This will take a hierarchy key ("|foo|bar") with tags ("|foo|bar!tagHash") and return an array with
102+
* each level in the hierarchy appended with the tags. ["foo!tagHash", "bar!tagHash"].
103+
*
104+
* @param string $key
105+
*
106+
* @return array
107+
*/
108+
private function explodeKey($string)
109+
{
110+
list($key, $tag) = explode(TaggablePoolInterface::TAG_SEPARATOR, $string.TaggablePoolInterface::TAG_SEPARATOR);
111+
112+
if ($key === HierarchicalPoolInterface::HIERARCHY_SEPARATOR) {
113+
$parts = ['root'];
114+
} else {
115+
$parts = explode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $key);
116+
// remove first element since it is always empty and replace it with 'root'
117+
$parts[0] = 'root';
118+
}
119+
120+
return array_map(function ($level) use ($tag) {
121+
return $level.TaggablePoolInterface::TAG_SEPARATOR.$tag;
122+
}, $parts);
123+
}
124+
}

Diff for: src/HierarchicalPoolInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
<?php
22

3+
/*
4+
* This file is part of php-cache\hierarchical-cache package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
312
namespace Cache\Hierarchy;
413

514
interface HierarchicalPoolInterface
615
{
16+
const HIERARCHY_SEPARATOR = '|';
717
}

0 commit comments

Comments
 (0)