Skip to content

Commit 69e9052

Browse files
KorbeilNyholm
authored andcommitted
PHPArray adapter using arrays for hierarchical (#219)
* fixing phpdoc * fixing phpdoc * adding const for Hierarchical modes * implementing modes in HierarchicalCachePoolTrait * switching to array mode for PHPArray implementation * fixing unaltered keys in array mode, they are forced in array * adding utility functions & using arrays for Hierarchical implementation * updating arrayIsset to manage nested cases * fixing failed test when having non-array return * using self as accessor for const * cleaning arrayIsset func to cacheIsset * cleaning arrayToolkit func to cacheToolkit * comment * updating hierarchical traits & adding one more to have keys as arrays (+ removing hierarchical modes) * implementing new HierarchicalArray trait for PHPArray adapter * fixing StyleCI * updating Changelogs * cleaning Changelog files * removing trait & implementing getHierarchyKey in PHPArray * removing tests encryption scrap
1 parent 45fa502 commit 69e9052

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

ArrayCachePool.php

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,17 @@ class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterf
3232
private $cache;
3333

3434
/**
35-
* @type array
36-
* A map to hold keys
35+
* @type array A map to hold keys
3736
*/
3837
private $keyMap = [];
3938

4039
/**
41-
* @type int
42-
* The maximum number of keys in the map
40+
* @type int The maximum number of keys in the map
4341
*/
4442
private $limit;
4543

4644
/**
47-
* @type int
48-
* The next key that we should remove from the cache
45+
* @type int The next key that we should remove from the cache
4946
*/
5047
private $currentPosition = 0;
5148

@@ -80,12 +77,14 @@ protected function getItemWithoutGenerateCacheKey($key)
8077
*/
8178
protected function fetchObjectFromCache($key)
8279
{
83-
$keyString = $this->getHierarchyKey($key);
84-
if (!isset($this->cache[$keyString])) {
80+
$keys = $this->getHierarchyKey($key);
81+
82+
if (!$this->cacheIsset($keys)) {
8583
return [false, null, [], null];
8684
}
8785

88-
list($data, $tags, $timestamp) = $this->cache[$keyString];
86+
$element = $this->cacheToolkit($keys);
87+
list($data, $tags, $timestamp) = $element;
8988

9089
if (is_object($data)) {
9190
$data = clone $data;
@@ -110,16 +109,10 @@ protected function clearAllObjectsFromCache()
110109
protected function clearOneObjectFromCache($key)
111110
{
112111
$this->commit();
113-
$path = null;
114-
$keyString = $this->getHierarchyKey($key, $path);
115-
if (isset($this->cache[$path])) {
116-
$this->cache[$path]++;
117-
} else {
118-
$this->cache[$path] = 0;
119-
}
120-
$this->clearHierarchyKeyCache();
112+
$keys = $this->getHierarchyKey($key);
121113

122-
unset($this->cache[$keyString]);
114+
$this->clearHierarchyKeyCache();
115+
$this->cacheToolkit($keys, null, true);
123116

124117
return true;
125118
}
@@ -129,12 +122,12 @@ protected function clearOneObjectFromCache($key)
129122
*/
130123
protected function storeItemInCache(PhpCacheItem $item, $ttl)
131124
{
132-
$key = $this->getHierarchyKey($item->getKey());
133-
$value = $item->get();
125+
$keys = $this->getHierarchyKey($item->getKey());
126+
$value = $item->get();
134127
if (is_object($value)) {
135128
$value = clone $value;
136129
}
137-
$this->cache[$key] = [$value, $item->getTags(), $item->getExpirationTimestamp()];
130+
$this->cacheToolkit($keys, [$value, $item->getTags(), $item->getExpirationTimestamp()]);
138131

139132
if ($this->limit !== null) {
140133
// Remove the oldest value
@@ -143,7 +136,7 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl)
143136
}
144137

145138
// Add the new key to the current position
146-
$this->keyMap[$this->currentPosition] = $key;
139+
$this->keyMap[$this->currentPosition] = implode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $keys);
147140

148141
// Increase the current position
149142
$this->currentPosition = ($this->currentPosition + 1) % $this->limit;
@@ -205,4 +198,77 @@ protected function removeListItem($name, $key)
205198
}
206199
}
207200
}
201+
202+
/**
203+
* Used to manipulate cached data by extracting, inserting or deleting value.
204+
*
205+
* @param array $keys
206+
* @param null|mixed $value
207+
* @param bool $unset
208+
*
209+
* @return mixed
210+
*/
211+
private function cacheToolkit($keys, $value = null, $unset = false)
212+
{
213+
$element = &$this->cache;
214+
215+
while ($keys && ($key = array_shift($keys))) {
216+
if (!$keys && is_null($value) && $unset) {
217+
unset($element[$key]);
218+
unset($element);
219+
$element = null;
220+
} else {
221+
$element =&$element[$key];
222+
}
223+
}
224+
225+
if (!$unset && !is_null($value)) {
226+
$element = $value;
227+
}
228+
229+
return $element;
230+
}
231+
232+
/**
233+
* Checking if given keys exists and is valid.
234+
*
235+
* @param array $keys
236+
*
237+
* @return bool
238+
*/
239+
private function cacheIsset($keys)
240+
{
241+
$has = false;
242+
$array = $this->cache;
243+
244+
foreach ($keys as $key) {
245+
if ($has = array_key_exists($key, $array)) {
246+
$array = $array[$key];
247+
}
248+
}
249+
250+
if (is_array($array)) {
251+
$has = $has && array_key_exists(0, $array);
252+
}
253+
254+
return $has;
255+
}
256+
257+
/**
258+
* Get a key to use with the hierarchy. If the key does not start with HierarchicalPoolInterface::SEPARATOR
259+
* this will return an unalterered key. This function supports a tagged key. Ie "foo:bar".
260+
* With this overwrite we'll return array as keys.
261+
*
262+
* @param string $key The original key
263+
*
264+
* @return array
265+
*/
266+
protected function getHierarchyKey($key)
267+
{
268+
if (!$this->isHierarchyKey($key)) {
269+
return [$key];
270+
}
271+
272+
return $this->explodeKey($key);
273+
}
208274
}

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## UNRELEASED
66

7+
### Changed
8+
9+
* New Hierarchical mode by using nested arrays instead of a flat array
10+
711
## 1.1.0
812

913
### Fixed

0 commit comments

Comments
 (0)