Skip to content

Commit

Permalink
Optimized CacheMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Dec 15, 2018
1 parent 9a3be90 commit fa4e665
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/CacheMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,26 @@ class CacheMap

public function get($key)
{
$index = $this->getPromiseCacheIndexByKey($key);
if (null === $index) {
return null;
}
$key = self::serializedKey($key);

return $this->promiseCache[$index]['promise'];
return isset($this->promiseCache[$key]) ? $this->promiseCache[$key] : null;
}

public function has($key)
{
$index = $this->getPromiseCacheIndexByKey($key);
if (null === $index) {
return false;
}

return true;
return isset($this->promiseCache[self::serializedKey($key)]);
}

public function set($key, $promise)
{
$this->promiseCache[] = [
'key' => $key,
'promise' => $promise,
];
$this->promiseCache[self::serializedKey($key)] = $promise;

return $this;
}

public function clear($key)
{
$index = $this->getPromiseCacheIndexByKey($key);
unset($this->promiseCache[$index]);
unset($this->promiseCache[self::serializedKey($key)]);

return $this;
}
Expand All @@ -60,13 +48,14 @@ public function clearAll()
return $this;
}

private function getPromiseCacheIndexByKey($cacheKey)
private static function serializedKey($key)
{
foreach ($this->promiseCache as $index => $data) {
if ($data['key'] === $cacheKey) {
return $index;
}
if (is_object($key)) {
return spl_object_hash($key);
} elseif (is_array($key)) {
return json_encode($key);
}
return null;

return $key;
}
}

0 comments on commit fa4e665

Please sign in to comment.