[Cache] Add LRU + max-lifetime capabilities to ArrayCache

This commit is contained in:
Nicolas Grekas 2020-01-16 14:19:26 +01:00
parent b350c80fc3
commit 48a5d5e8a9
3 changed files with 115 additions and 9 deletions

View File

@ -15,10 +15,15 @@ use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface; use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\CacheInterface;
/** /**
* An in-memory cache storage.
*
* Acts as a least-recently-used (LRU) storage when configured with a maximum number of items.
*
* @author Nicolas Grekas <p@tchwork.com> * @author Nicolas Grekas <p@tchwork.com>
*/ */
class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
@ -29,13 +34,25 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
private $values = []; private $values = [];
private $expiries = []; private $expiries = [];
private $createCacheItem; private $createCacheItem;
private $maxLifetime;
private $maxItems;
/** /**
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
*/ */
public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true) public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true, int $maxLifetime = 0, int $maxItems = 0)
{ {
if (0 > $maxLifetime) {
throw new InvalidArgumentException(sprintf('Argument $maxLifetime must be a positive integer, %d passed.', $maxLifetime));
}
if (0 > $maxItems) {
throw new InvalidArgumentException(sprintf('Argument $maxItems must be a positive integer, %d passed.', $maxItems));
}
$this->storeSerialized = $storeSerialized; $this->storeSerialized = $storeSerialized;
$this->maxLifetime = $maxLifetime;
$this->maxItems = $maxItems;
$this->createCacheItem = \Closure::bind( $this->createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) use ($defaultLifetime) { static function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem(); $item = new CacheItem();
@ -84,6 +101,13 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
public function hasItem($key) public function hasItem($key)
{ {
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) { if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
if ($this->maxItems) {
// Move the item last in the storage
$value = $this->values[$key];
unset($this->values[$key]);
$this->values[$key] = $value;
}
return true; return true;
} }
CacheItem::validateKey($key); CacheItem::validateKey($key);
@ -97,7 +121,12 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
public function getItem($key) public function getItem($key)
{ {
if (!$isHit = $this->hasItem($key)) { if (!$isHit = $this->hasItem($key)) {
$this->values[$key] = $value = null; $value = null;
if (!$this->maxItems) {
// Track misses in non-LRU mode only
$this->values[$key] = null;
}
} else { } else {
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key]; $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
} }
@ -164,7 +193,9 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
$value = $item["\0*\0value"]; $value = $item["\0*\0value"];
$expiry = $item["\0*\0expiry"]; $expiry = $item["\0*\0expiry"];
if (null !== $expiry && $expiry <= microtime(true)) { $now = microtime(true);
if (null !== $expiry && $expiry <= $now) {
$this->deleteItem($key); $this->deleteItem($key);
return true; return true;
@ -173,7 +204,23 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
return false; return false;
} }
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) { if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = microtime(true) + $item["\0*\0defaultLifetime"]; $expiry = $item["\0*\0defaultLifetime"];
$expiry = $now + ($expiry > ($this->maxLifetime ?: $expiry) ? $this->maxLifetime : $expiry);
} elseif ($this->maxLifetime && (null === $expiry || $expiry > $now + $this->maxLifetime)) {
$expiry = $now + $this->maxLifetime;
}
if ($this->maxItems) {
unset($this->values[$key]);
// Iterate items and vacuum expired ones while we are at it
foreach ($this->values as $k => $v) {
if ($this->expiries[$k] > $now && \count($this->values) < $this->maxItems) {
break;
}
unset($this->values[$k], $this->expiries[$k]);
}
} }
$this->values[$key] = $value; $this->values[$key] = $value;
@ -210,15 +257,21 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
public function clear(string $prefix = '') public function clear(string $prefix = '')
{ {
if ('' !== $prefix) { if ('' !== $prefix) {
$now = microtime(true);
foreach ($this->values as $key => $value) { foreach ($this->values as $key => $value) {
if (0 === strpos($key, $prefix)) { if (!isset($this->expiries[$key]) || $this->expiries[$key] <= $now || 0 === strpos($key, $prefix)) {
unset($this->values[$key], $this->expiries[$key]); unset($this->values[$key], $this->expiries[$key]);
} }
} }
} else {
$this->values = $this->expiries = []; if ($this->values) {
return true;
}
} }
$this->values = $this->expiries = [];
return true; return true;
} }
@ -258,8 +311,20 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
{ {
foreach ($keys as $i => $key) { foreach ($keys as $i => $key) {
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) { if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) {
$this->values[$key] = $value = null; $value = null;
if (!$this->maxItems) {
// Track misses in non-LRU mode only
$this->values[$key] = null;
}
} else { } else {
if ($this->maxItems) {
// Move the item last in the storage
$value = $this->values[$key];
unset($this->values[$key]);
$this->values[$key] = $value;
}
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key]; $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
} }
unset($keys[$i]); unset($keys[$i]);
@ -314,8 +379,12 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
$value = false; $value = false;
} }
if (false === $value) { if (false === $value) {
$this->values[$key] = $value = null; $value = null;
$isHit = false; $isHit = false;
if (!$this->maxItems) {
$this->values[$key] = null;
}
} }
} }

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
5.1.0
-----
* added max-items + LRU + max-lifetime capabilities to `ArrayCache`
5.0.0 5.0.0
----- -----

View File

@ -55,4 +55,36 @@ class ArrayAdapterTest extends AdapterTestCase
$this->assertArrayHasKey('bar', $values); $this->assertArrayHasKey('bar', $values);
$this->assertNull($values['bar']); $this->assertNull($values['bar']);
} }
public function testMaxLifetime()
{
$cache = new ArrayAdapter(0, false, 1);
$item = $cache->getItem('foo');
$item->expiresAfter(2);
$cache->save($item->set(123));
$this->assertTrue($cache->hasItem('foo'));
sleep(1);
$this->assertFalse($cache->hasItem('foo'));
}
public function testMaxItems()
{
$cache = new ArrayAdapter(0, false, 0, 2);
$cache->save($cache->getItem('foo'));
$cache->save($cache->getItem('bar'));
$cache->save($cache->getItem('buz'));
$this->assertFalse($cache->hasItem('foo'));
$this->assertTrue($cache->hasItem('bar'));
$this->assertTrue($cache->hasItem('buz'));
$cache->save($cache->getItem('foo'));
$this->assertFalse($cache->hasItem('bar'));
$this->assertTrue($cache->hasItem('buz'));
$this->assertTrue($cache->hasItem('foo'));
}
} }