Skip to content

Commit

Permalink
PHPArray adapter using arrays for hierarchical (#219)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Korbeil authored and Nyholm committed Jul 11, 2018
1 parent 45fa502 commit 69e9052
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 22 deletions.
110 changes: 88 additions & 22 deletions ArrayCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterf
private $cache;

/**
* @type array
* A map to hold keys
* @type array A map to hold keys
*/
private $keyMap = [];

/**
* @type int
* The maximum number of keys in the map
* @type int The maximum number of keys in the map
*/
private $limit;

/**
* @type int
* The next key that we should remove from the cache
* @type int The next key that we should remove from the cache
*/
private $currentPosition = 0;

Expand Down Expand Up @@ -80,12 +77,14 @@ protected function getItemWithoutGenerateCacheKey($key)
*/
protected function fetchObjectFromCache($key)
{
$keyString = $this->getHierarchyKey($key);
if (!isset($this->cache[$keyString])) {
$keys = $this->getHierarchyKey($key);

if (!$this->cacheIsset($keys)) {
return [false, null, [], null];
}

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

if (is_object($data)) {
$data = clone $data;
Expand All @@ -110,16 +109,10 @@ protected function clearAllObjectsFromCache()
protected function clearOneObjectFromCache($key)
{
$this->commit();
$path = null;
$keyString = $this->getHierarchyKey($key, $path);
if (isset($this->cache[$path])) {
$this->cache[$path]++;
} else {
$this->cache[$path] = 0;
}
$this->clearHierarchyKeyCache();
$keys = $this->getHierarchyKey($key);

unset($this->cache[$keyString]);
$this->clearHierarchyKeyCache();
$this->cacheToolkit($keys, null, true);

return true;
}
Expand All @@ -129,12 +122,12 @@ protected function clearOneObjectFromCache($key)
*/
protected function storeItemInCache(PhpCacheItem $item, $ttl)
{
$key = $this->getHierarchyKey($item->getKey());
$value = $item->get();
$keys = $this->getHierarchyKey($item->getKey());
$value = $item->get();
if (is_object($value)) {
$value = clone $value;
}
$this->cache[$key] = [$value, $item->getTags(), $item->getExpirationTimestamp()];
$this->cacheToolkit($keys, [$value, $item->getTags(), $item->getExpirationTimestamp()]);

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

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

// Increase the current position
$this->currentPosition = ($this->currentPosition + 1) % $this->limit;
Expand Down Expand Up @@ -205,4 +198,77 @@ protected function removeListItem($name, $key)
}
}
}

/**
* Used to manipulate cached data by extracting, inserting or deleting value.
*
* @param array $keys
* @param null|mixed $value
* @param bool $unset
*
* @return mixed
*/
private function cacheToolkit($keys, $value = null, $unset = false)
{
$element = &$this->cache;

while ($keys && ($key = array_shift($keys))) {
if (!$keys && is_null($value) && $unset) {
unset($element[$key]);
unset($element);
$element = null;
} else {
$element =&$element[$key];
}
}

if (!$unset && !is_null($value)) {
$element = $value;
}

return $element;
}

/**
* Checking if given keys exists and is valid.
*
* @param array $keys
*
* @return bool
*/
private function cacheIsset($keys)
{
$has = false;
$array = $this->cache;

foreach ($keys as $key) {
if ($has = array_key_exists($key, $array)) {
$array = $array[$key];
}
}

if (is_array($array)) {
$has = $has && array_key_exists(0, $array);
}

return $has;
}

/**
* 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".
* With this overwrite we'll return array as keys.
*
* @param string $key The original key
*
* @return array
*/
protected function getHierarchyKey($key)
{
if (!$this->isHierarchyKey($key)) {
return [$key];
}

return $this->explodeKey($key);
}
}
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee

## UNRELEASED

### Changed

* New Hierarchical mode by using nested arrays instead of a flat array

## 1.1.0

### Fixed
Expand Down

0 comments on commit 69e9052

Please sign in to comment.