Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Cache] Use the default expiry when saving (not when creating) items #37562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2020
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
9 changes: 5 additions & 4 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ protected function __construct($namespace = '', $defaultLifetime = 0)
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
}
$this->createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) use ($defaultLifetime) {
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->isHit = $isHit;
$item->defaultLifetime = $defaultLifetime;

return $item;
},
Expand All @@ -62,14 +61,16 @@ static function ($key, $value, $isHit) use ($defaultLifetime) {
);
$getId = function ($key) { return $this->getId((string) $key); };
$this->mergeByLifetime = \Closure::bind(
static function ($deferred, $namespace, &$expiredIds) use ($getId) {
static function ($deferred, $namespace, &$expiredIds) use ($getId, $defaultLifetime) {
$byLifetime = [];
$now = time();
$expiredIds = [];

foreach ($deferred as $key => $item) {
if (null === $item->expiry) {
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$getId($key)] = $item->value;
$byLifetime[0 < $defaultLifetime ? $defaultLifetime : 0][$getId($key)] = $item->value;
} elseif (0 === $item->expiry) {
$byLifetime[0][$getId($key)] = $item->value;
} elseif ($item->expiry > $now) {
$byLifetime[$item->expiry - $now][$getId($key)] = $item->value;
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, Resettable
use ArrayTrait;

private $createCacheItem;
private $defaultLifetime;

/**
* @param int $defaultLifetime
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
*/
public function __construct($defaultLifetime = 0, $storeSerialized = true)
{
$this->defaultLifetime = $defaultLifetime;
$this->storeSerialized = $storeSerialized;
$this->createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) use ($defaultLifetime) {
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->isHit = $isHit;
$item->defaultLifetime = $defaultLifetime;

return $item;
},
Expand Down Expand Up @@ -127,8 +128,8 @@ public function save(CacheItemInterface $item)
return false;
}
}
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = time() + $item["\0*\0defaultLifetime"];
if (null === $expiry && 0 < $this->defaultLifetime) {
$expiry = time() + $this->defaultLifetime;
}

$this->values[$key] = $value;
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ public function __construct(array $adapters, $defaultLifetime = 0)
$this->syncItem = \Closure::bind(
static function ($sourceItem, $item) use ($defaultLifetime) {
$item->value = $sourceItem->value;
$item->expiry = $sourceItem->expiry;
$item->isHit = $sourceItem->isHit;

if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
$defaultLifetime = $sourceItem->defaultLifetime;
}
if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
$item->defaultLifetime = $defaultLifetime;
if (0 < $defaultLifetime) {
$item->expiresAfter($defaultLifetime);
}

return $item;
Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableIn
private $namespaceLen;
private $createCacheItem;
private $poolHash;
private $defaultLifetime;

/**
* @param string $namespace
Expand All @@ -40,11 +41,11 @@ public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defa
$this->poolHash = $poolHash = spl_object_hash($pool);
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace);
$this->namespaceLen = \strlen($namespace);
$this->defaultLifetime = $defaultLifetime;
$this->createCacheItem = \Closure::bind(
static function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
static function ($key, $innerItem) use ($poolHash) {
$item = new CacheItem();
$item->key = $key;
$item->defaultLifetime = $defaultLifetime;
$item->poolHash = $poolHash;

if (null !== $innerItem) {
Expand Down Expand Up @@ -155,8 +156,8 @@ private function doSave(CacheItemInterface $item, $method)
}
$item = (array) $item;
$expiry = $item["\0*\0expiry"];
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = time() + $item["\0*\0defaultLifetime"];
if (null === $expiry && 0 < $this->defaultLifetime) {
$expiry = time() + $this->defaultLifetime;
}

if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static function ($key, $value, CacheItem $protoItem) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->defaultLifetime = $protoItem->defaultLifetime;
$item->expiry = $protoItem->expiry;
$item->poolHash = $protoItem->poolHash;

Expand Down Expand Up @@ -90,8 +89,7 @@ static function ($deferred) {
$this->invalidateTags = \Closure::bind(
static function (AdapterInterface $tagsAdapter, array $tags) {
foreach ($tags as $v) {
$v->defaultLifetime = 0;
$v->expiry = null;
$v->expiry = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does "0" mean "infinity"? then there is a small glitch if one sets 0 as the timestamp for expiry, which will be confused with "infinity".
Should/can we use "-1" for this instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, according to \Symfony\Component\Cache\Traits\AbstractTrait::doSave() "0" means "infinity". Also, $defaultLifetime = 0 in adapter's constructors mean the same.

From a user point of view there are only ways to specify expiration:

  • $defaultLifetime in adapter. And there 0 is already mean "infinity"
  • CacheItemInterface::expiresAfter(). There 0 means number of seconds, so I guess it shouldn't confuse user.
  • CacheItemInterface::expiresAt(). Its impossible to pass 0 here.

BTW, this is only place where $item->expiry is set to "0" explicitly, because we want to ignore adapter's default lifetime here if any is set when store tags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CacheItemInterface::expiresAt(). Its impossible to pass 0 here.

got it: the unix epoch time here is 0.0, not 0.

$tagsAdapter->saveDeferred($v);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ final class CacheItem implements CacheItemInterface
protected $value;
protected $isHit = false;
protected $expiry;
protected $defaultLifetime;
protected $tags = [];
protected $prevTags = [];
protected $innerItem;
Expand Down Expand Up @@ -74,7 +73,7 @@ public function set($value)
public function expiresAt($expiration)
{
if (null === $expiration) {
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
$this->expiry = null;
} elseif ($expiration instanceof \DateTimeInterface) {
$this->expiry = (int) $expiration->format('U');
} else {
Expand All @@ -92,7 +91,7 @@ public function expiresAt($expiration)
public function expiresAfter($time)
{
if (null === $time) {
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
$this->expiry = null;
} elseif ($time instanceof \DateInterval) {
$this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
} elseif (\is_int($time)) {
Expand Down
120 changes: 119 additions & 1 deletion src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ChainAdapterTest extends AdapterTestCase
{
public function createCachePool($defaultLifetime = 0)
{
return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter($defaultLifetime), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
}

public function testEmptyAdaptersException()
Expand Down Expand Up @@ -65,6 +65,124 @@ public function testPrune()
$this->assertFalse($cache->prune());
}

public function testMultipleCachesExpirationWhenCommonTtlIsNotSet()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$adapter1 = new ArrayAdapter(4);
$adapter2 = new ArrayAdapter(2);

$cache = new ChainAdapter([$adapter1, $adapter2]);

$cache->save($cache->getItem('key')->set('value'));

$item = $adapter1->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

$item = $adapter2->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

sleep(2);

$item = $adapter1->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

$item = $adapter2->getItem('key');
$this->assertFalse($item->isHit());

sleep(2);

$item = $adapter1->getItem('key');
$this->assertFalse($item->isHit());

$adapter2->save($adapter2->getItem('key1')->set('value1'));

$item = $cache->getItem('key1');
$this->assertTrue($item->isHit());
$this->assertEquals('value1', $item->get());

sleep(2);

$item = $adapter1->getItem('key1');
$this->assertTrue($item->isHit());
$this->assertEquals('value1', $item->get());

$item = $adapter2->getItem('key1');
$this->assertFalse($item->isHit());

sleep(2);

$item = $adapter1->getItem('key1');
$this->assertFalse($item->isHit());
}

public function testMultipleCachesExpirationWhenCommonTtlIsSet()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$adapter1 = new ArrayAdapter(4);
$adapter2 = new ArrayAdapter(2);

$cache = new ChainAdapter([$adapter1, $adapter2], 6);

$cache->save($cache->getItem('key')->set('value'));

$item = $adapter1->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

$item = $adapter2->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

sleep(2);

$item = $adapter1->getItem('key');
$this->assertTrue($item->isHit());
$this->assertEquals('value', $item->get());

$item = $adapter2->getItem('key');
$this->assertFalse($item->isHit());

sleep(2);

$item = $adapter1->getItem('key');
$this->assertFalse($item->isHit());

$adapter2->save($adapter2->getItem('key1')->set('value1'));

$item = $cache->getItem('key1');
$this->assertTrue($item->isHit());
$this->assertEquals('value1', $item->get());

sleep(2);

$item = $adapter1->getItem('key1');
$this->assertTrue($item->isHit());
$this->assertEquals('value1', $item->get());

$item = $adapter2->getItem('key1');
$this->assertFalse($item->isHit());

sleep(2);

$item = $adapter1->getItem('key1');
$this->assertTrue($item->isHit());
$this->assertEquals('value1', $item->get());

sleep(2);

$item = $adapter1->getItem('key1');
$this->assertFalse($item->isHit());
}

/**
* @return MockObject|PruneableCacheInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class ExternalAdapter implements CacheItemPoolInterface
{
private $cache;

public function __construct()
public function __construct($defaultLifetime = 0)
{
$this->cache = new ArrayAdapter();
$this->cache = new ArrayAdapter($defaultLifetime);
}

public function getItem($key)
Expand Down