feature #35362 [Cache] Add LRU + max-lifetime capabilities to ArrayCache (nicolas-grekas)
This PR was merged into the 5.1-dev branch.
Discussion
----------
[Cache] Add LRU + max-lifetime capabilities to ArrayCache
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix https://github.com/orgs/symfony/projects/1#card-30686676
| License | MIT
| Doc PR | -
In https://github.com/symfony/symfony/pull/32294#issuecomment-508067745, @andrerom writes:
> if you plan to expose use of ArrayAdapter to a wider audience you should probably also add the following features to it:
> - max item limit to avoid reaching memory limits
> - own (very low, like default 100-500ms) TTL for in-memory caching, as it's in practice stale data when used in concurrent scenarios
>
> If you want to be advance you can also:
>
> - keep track of use, and evict cache items based on that using LFU when reaching limit
> - in-memory cache is domain & project specific in terms of how long it's somewhat "safe" to keep items in memory, so either describe when to use and not use on a per pool term, or allow use of pool to pass in flags to opt out of in-memory cache for cases developer knows it should be ignored
This PR implements these suggestions, via two new constructor arguments: `$maxLifetime` and `$maxItems`.
In Yaml:
```yaml
services:
app.lru150_cache:
parent: cache.adapter.array
arguments:
$maxItems: 150
$maxLifetime: 0.150
framework:
cache:
pools:
my_chained_pool:
adapters:
- app.lru150_cache
- cache.adapter.filesystem
```
This configuration adds a local memory cache that keeps max 150 elements for 150ms on top of a filesystem cache.
/cc @lyrixx since you were also interested in it.
Commits
-------
48a5d5e8a9
[Cache] Add LRU + max-lifetime capabilities to ArrayCache
This commit is contained in:
commit
b22a58449c
@ -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,14 +257,20 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
5.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added max-items + LRU + max-lifetime capabilities to `ArrayCache`
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user