1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
<?php
namespace Webmozart\KeyValueStore\Decorator;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\FlushableCache;
use InvalidArgumentException;
use Webmozart\KeyValueStore\Api\KeyValueStore;
use Webmozart\KeyValueStore\Api\NoSuchKeyException;
class CachingDecorator extends AbstractDecorator
{
private $cache;
private $ttl;
public function __construct(KeyValueStore $store, Cache $cache, $ttl = 0)
{
if (!$cache instanceof ClearableCache && !$cache instanceof FlushableCache) {
throw new InvalidArgumentException(sprintf(
'The cache must either implement ClearableCache or '.
'FlushableCache. Got: %s',
get_class($cache)
));
}
parent::__construct($store);
$this->cache = $cache;
$this->ttl = $ttl;
}
public function set($key, $value)
{
$this->store->set($key, $value);
$this->cache->save($key, $value, $this->ttl);
}
public function get($key, $default = null)
{
if ($this->cache->contains($key)) {
return $this->cache->fetch($key);
}
try {
$value = $this->store->getOrFail($key);
} catch (NoSuchKeyException $e) {
return $default;
}
$this->cache->save($key, $value, $this->ttl);
return $value;
}
public function getOrFail($key)
{
if ($this->cache->contains($key)) {
return $this->cache->fetch($key);
}
$value = $this->store->getOrFail($key);
$this->cache->save($key, $value, $this->ttl);
return $value;
}
public function getMultiple(array $keys, $default = null)
{
$values = array();
foreach ($keys as $i => $key) {
if ($this->cache->contains($key)) {
$values[$key] = $this->cache->fetch($key);
unset($keys[$i]);
}
}
return array_replace($values, $this->store->getMultiple($keys, $default));
}
public function getMultipleOrFail(array $keys)
{
$values = array();
foreach ($keys as $i => $key) {
if ($this->cache->contains($key)) {
$values[$key] = $this->cache->fetch($key);
unset($keys[$i]);
}
}
$values = array_replace($values, $this->store->getMultipleOrFail($keys));
foreach ($keys as $key) {
$this->cache->save($key, $values[$key], $this->ttl);
}
return $values;
}
public function remove($key)
{
$this->store->remove($key);
$this->cache->delete($key);
}
public function exists($key)
{
if ($this->cache->contains($key)) {
return true;
}
return $this->store->exists($key);
}
public function clear()
{
$this->store->clear();
if ($this->cache instanceof ClearableCache) {
$this->cache->deleteAll();
} else {
$this->cache->flushAll();
}
}
}