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

Skip to content

Commit 8f8af9c

Browse files
committed
[Cache] Fix saving items with no expiration through ProxyAdapter
1 parent ee211c4 commit 8f8af9c

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static function ($deferred, $namespace, &$expiredIds) use ($getId, $defaultLifet
7474
$key = (string) $key;
7575
if (null === $item->expiry) {
7676
$ttl = 0 < $defaultLifetime ? $defaultLifetime : 0;
77-
} elseif (0 === $item->expiry) {
77+
} elseif (!$item->expiry) {
7878
$ttl = 0;
7979
} elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) {
8080
$expiredIds[] = $getId($key);

src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static function ($deferred, &$expiredIds) use ($getId, $tagPrefix, $defaultLifet
8080
$key = (string) $key;
8181
if (null === $item->expiry) {
8282
$ttl = 0 < $defaultLifetime ? $defaultLifetime : 0;
83-
} elseif (0 === $item->expiry) {
83+
} elseif (!$item->expiry) {
8484
$ttl = 0;
8585
} elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) {
8686
$expiredIds[] = $getId($key);

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ public function save(CacheItemInterface $item)
124124
$value = $item["\0*\0value"];
125125
$expiry = $item["\0*\0expiry"];
126126

127-
if (0 === $expiry) {
128-
$expiry = \PHP_INT_MAX;
129-
}
130-
131-
if (null !== $expiry && $expiry <= microtime(true)) {
132-
$this->deleteItem($key);
127+
if (null !== $expiry) {
128+
if (!$expiry) {
129+
$expiry = \PHP_INT_MAX;
130+
} elseif ($expiry <= microtime(true)) {
131+
$this->deleteItem($key);
133132

134-
return true;
133+
return true;
134+
}
135135
}
136136
if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
137137
return false;

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static function (CacheItemInterface $innerItem, array $item) {
8888
$item["\0*\0value"] = ["\x9D".pack('VN', (int) (0.1 + $metadata[self::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[self::METADATA_CTIME])."\x5F" => $item["\0*\0value"]];
8989
}
9090
$innerItem->set($item["\0*\0value"]);
91-
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6F', 0 === $item["\0*\0expiry"] ? \PHP_INT_MAX : $item["\0*\0expiry"])) : null);
91+
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6F', $item["\0*\0expiry"])) : null);
9292
},
9393
null,
9494
CacheItem::class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\Cache\Tests\Adapter;
5+
6+
use Psr\Cache\CacheItemPoolInterface;
7+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
8+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
9+
use Symfony\Component\Cache\Adapter\RedisAdapter;
10+
use Symfony\Component\Cache\CacheItem;
11+
12+
/**
13+
* @group integration
14+
*/
15+
class ProxyAdapterAndRedisAdapterTest extends AbstractRedisAdapterTest
16+
{
17+
protected $skippedTests = [
18+
'testPrune' => 'RedisAdapter does not implement PruneableInterface.',
19+
];
20+
21+
public static function setUpBeforeClass(): void
22+
{
23+
parent::setUpBeforeClass();
24+
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'));;
25+
}
26+
27+
public function createCachePool($defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
28+
{
29+
return new ProxyAdapter(new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), 100), 'ProxyNS', $defaultLifetime);
30+
}
31+
32+
public function testSaveItemPermanently()
33+
{
34+
$setCacheItemExpiry = \Closure::bind(
35+
static function (CacheItem $item, $expiry) {
36+
$item->expiry = $expiry;
37+
38+
return $item;
39+
},
40+
null,
41+
CacheItem::class
42+
);
43+
44+
$cache = $this->createCachePool(1);
45+
$value = rand();
46+
$item = $cache->getItem('foo');
47+
$setCacheItemExpiry($item, 0);
48+
$cache->save($item->set($value));
49+
$item = $cache->getItem('bar');
50+
$setCacheItemExpiry($item, 0.0);
51+
$cache->save($item->set($value));
52+
$item = $cache->getItem('baz');
53+
$cache->save($item->set($value));
54+
55+
$this->assertSame($value, $this->cache->getItem('foo')->get());
56+
$this->assertSame($value, $this->cache->getItem('bar')->get());
57+
$this->assertSame($value, $this->cache->getItem('baz')->get());
58+
59+
sleep(1);
60+
$this->assertSame($value, $this->cache->getItem('foo')->get());
61+
$this->assertSame($value, $this->cache->getItem('bar')->get());
62+
$this->assertFalse($this->cache->getItem('baz')->isHit());
63+
}
64+
}

0 commit comments

Comments
 (0)