Skip to content

Commit

Permalink
Improve caching functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 29, 2017
1 parent 668c980 commit 6731319
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 84 deletions.
41 changes: 15 additions & 26 deletions system/src/Grav/Framework/Cache/Adapter/ChainCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class ChainCache extends AbstractCache
*/
protected $count;

/**
* @var \stdClass
*/
protected $miss;

/**
* Chain Cache constructor.
* @param array $caches
Expand All @@ -54,14 +49,13 @@ public function __construct(array $caches, $defaultLifetime = null)

$this->caches = array_values($caches);
$this->count = count($caches);
$this->miss = new \stdClass;
}

public function doGet($key)
public function doGet($key, $miss)
{
foreach ($this->caches as $i => $cache) {
$value = $cache->doGet($key);
if ($this->isHit($value)) {
$value = $cache->doGet($key, $miss);
if ($value !== $miss) {
while (--$i >= 0) {
// Update all the previous caches with missing value.
$this->caches[$i]->doSet($key, $value, $this->getDefaultLifetime());
Expand All @@ -71,7 +65,7 @@ public function doGet($key)
}
}

return $this->miss();
return $miss;
}

public function doSet($key, $value, $ttl)
Expand Down Expand Up @@ -110,34 +104,29 @@ public function doClear()
}


public function doGetMultiple($keys)
public function doGetMultiple($keys, $miss)
{
$found = [];
$missing = [];
$list = [];
foreach ($this->caches as $i => $cache) {
$values = $cache->doGetMultiple($i ? $missing[$i - 1] : $keys);
$list[$i] = $cache->doGetMultiple($keys, $miss);

foreach ($values as $key => $value) {
if ($this->isHit($value)) {
$found[$key] = $value;
} else {
$missing[$i][$key] = true;
}
}
$keys = array_diff_key($keys, $list[$i]);

if (empty($missing[$i])) {
if (!$keys) {
break;
}
}

$values = [];
// Update all the previous caches with missing values.
foreach (array_reverse($missing) as $i => $keys) {
$values += array_intersect($found, $keys);
$this->caches[$i]->doSetMultiple($values, $this->getDefaultLifetime());
foreach (array_reverse($list) as $i => $items) {
$values += $items;
if ($i && $values) {
$this->caches[$i-1]->doSetMultiple($values, $this->getDefaultLifetime());
}
}

return $found;
return $values;
}

public function doSetMultiple($values, $ttl)
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function __construct(CacheProvider $doctrineCache, $namespace = '', $defa
$this->driver = $doctrineCache;
}

public function doGet($key)
public function doGet($key, $miss)
{
$value = $this->driver->fetch($key);

// Doctrine cache does not differentiate between no result and cached 'false'. Make sure that we do.
return $value !== false || $this->driver->contains($key) ? $value : $this->miss();
return $value !== false || $this->driver->contains($key) ? $value : $miss;
}

public function doSet($key, $value, $ttl)
Expand All @@ -64,7 +64,7 @@ public function doClear()
return $this->driver->deleteAll();
}

public function doGetMultiple($keys)
public function doGetMultiple($keys, $miss)
{
return $this->driver->fetchMultiple($keys);
}
Expand Down
9 changes: 4 additions & 5 deletions system/src/Grav/Framework/Cache/Adapter/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public function __construct()
parent::__construct();
}

public function doGet($key)
public function doGet($key, $miss)
{
if (!array_key_exists($key, $this->cache)) {
// Cache misses.
$this->cache[$key] = $this->miss();
return $miss;
}

return $this->cache[$key];
Expand All @@ -50,7 +49,7 @@ public function doSet($key, $value, $ttl)

public function doDelete($key)
{
$this->cache[$key] = $this->miss();
unset($this->cache[$key]);

return true;
}
Expand All @@ -64,6 +63,6 @@ public function doClear()

public function doHas($key)
{
return array_key_exists($key, $this->cache) && $this->isHit($this->cache[$key]);
return array_key_exists($key, $this->cache);
}
}
4 changes: 2 additions & 2 deletions system/src/Grav/Framework/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
interface CacheInterface extends SimpleCacheInterface
{
public function doGet($key);
public function doGet($key, $miss);
public function doSet($key, $value, $ttl);
public function doDelete($key);
public function doClear();
public function doGetMultiple($keys);
public function doGetMultiple($keys, $miss);
public function doSetMultiple($values, $ttl);
public function doDeleteMultiple($keys);
public function doHas($key);
Expand Down
20 changes: 0 additions & 20 deletions system/src/Grav/Framework/Cache/CacheMiss.php

This file was deleted.

44 changes: 16 additions & 28 deletions system/src/Grav/Framework/Cache/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait CacheTrait
private $defaultLifetime = null;

/**
* @var CacheMiss
* @var \stdClass
*/
private $miss;

Expand All @@ -42,7 +42,7 @@ protected function init($namespace = '', $defaultLifetime = null)
{
$this->namespace = (string) $namespace;
$this->defaultLifetime = $this->convertTtl($defaultLifetime, true);
$this->miss = new CacheMiss();
$this->miss = new \stdClass;
}

/**
Expand All @@ -61,33 +61,16 @@ protected function getDefaultLifetime()
return $this->defaultLifetime;
}

/**
* @param mixed $value
* @return bool
*/
protected function isHit($value)
{
return !($value instanceof CacheMiss);
}

/**
* @return CacheMiss
*/
protected function miss()
{
return $this->miss;
}

/**
* @inheritdoc
*/
public function get($key, $default = null)
{
$this->validateKey($key);

$value = $this->doGet($key);
$value = $this->doGet($key, $this->miss);

return $this->isHit($value) ? $value : $default;
return $value !== $this->miss ? $value : $default;
}

/**
Expand Down Expand Up @@ -137,12 +120,17 @@ public function getMultiple($keys, $default = null)
}

$this->validateKeys($keys);
$keys = array_unique($keys);
$keys = array_combine($keys, $keys);

$list = $this->doGetMultiple($keys);
$list = $this->doGetMultiple($keys, $this->miss);

if (count($list) !== count($keys)) {
// Return all values, with default value if they do not exist.
return array_replace(array_fill_keys($keys, $default), $list);
foreach ($keys as $key) {
if (!array_key_exists($key, $list) || $list[$key] === $this->miss) {
$list[$key] = $default;
}
}
}

// Make sure that results are returned in the same order as the keys were given.
Expand Down Expand Up @@ -204,18 +192,18 @@ public function has($key)
return $this->doHas($key);
}

abstract public function doGet($key);
abstract public function doGet($key, $miss);
abstract public function doSet($key, $value, $ttl);
abstract public function doDelete($key);
abstract public function doClear();

public function doGetMultiple($keys)
public function doGetMultiple($keys, $miss)
{
$results = [];

foreach ($keys as $key) {
$value = $this->doGet($key);
if ($this->isHit($value)) {
$value = $this->doGet($key, $miss);
if ($value !== $miss) {
$results[$key] = $value;
}
}
Expand Down

0 comments on commit 6731319

Please sign in to comment.