Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
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>
*/
class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
Expand All @@ -29,13 +34,25 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
private $values = [];
private $expiries = [];
private $createCacheItem;
private $maxLifetime;
private $maxItems;

/**
* @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->maxLifetime = $maxLifetime;
$this->maxItems = $maxItems;
$this->createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem();
Expand Down Expand Up @@ -84,6 +101,13 @@ public function delete(string $key): bool
public function hasItem($key)
{
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;
}
CacheItem::validateKey($key);
Expand All @@ -97,7 +121,12 @@ public function hasItem($key)
public function getItem($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 {
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
}
Expand Down Expand Up @@ -164,7 +193,9 @@ public function save(CacheItemInterface $item)
$value = $item["\0*\0value"];
$expiry = $item["\0*\0expiry"];

if (null !== $expiry && $expiry <= microtime(true)) {
$now = microtime(true);

if (null !== $expiry && $expiry <= $now) {
$this->deleteItem($key);

return true;
Expand All @@ -173,7 +204,23 @@ public function save(CacheItemInterface $item)
return false;
}
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;
Expand Down Expand Up @@ -210,15 +257,21 @@ public function commit()
public function clear(string $prefix = '')
{
if ('' !== $prefix) {
$now = microtime(true);

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]);
}
}
} else {
$this->values = $this->expiries = [];

if ($this->values) {
return true;
}
}

$this->values = $this->expiries = [];

return true;
}

Expand Down Expand Up @@ -258,8 +311,20 @@ private function generateItems(array $keys, $now, $f)
{
foreach ($keys as $i => $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 {
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];
}
unset($keys[$i]);
Expand Down Expand Up @@ -314,8 +379,12 @@ private function unfreeze(string $key, bool &$isHit)
$value = false;
}
if (false === $value) {
$this->values[$key] = $value = null;
$value = null;
$isHit = false;

if (!$this->maxItems) {
$this->values[$key] = null;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* added max-items + LRU + max-lifetime capabilities to `ArrayCache`

5.0.0
-----

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,36 @@ public function testGetValuesHitAndMiss()
$this->assertArrayHasKey('bar', $values);
$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'));
}
}