8000 Merge branch '4.4' into 5.3 · symfony/cache@3ed2049 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ed2049

Browse files
Merge branch '4.4' into 5.3
* 4.4: [Cache] Fix saving items with no expiration through ProxyAdapter CS fixes [Cache] disable lock on CLI [VarDumper] add more "transient-on-macos" groups
2 parents 71ab76b + 0ff0a73 commit 3ed2049

File tree

7 files changed

+96
-32
lines changed

7 files changed

+96
-32
lines changed

Adapter/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static function ($deferred, $namespace, &$expiredIds, $getId, $defaultLifetime)
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);

Adapter/AbstractTagAwareAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static function ($deferred, &$expiredIds, $getId, $tagPrefix, $defaultLifetime)
7979
$key = (string) $key;
8080
if (null === $item->expiry) {
8181
$ttl = 0 < $defaultLifetime ? $defaultLifetime : 0;
82-
} elseif (0 === $item->expiry) {
82+
} elseif (!$item->expiry) {
8383
$ttl = 0;
8484
} elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) {
8585
$expiredIds[] = $getId($key);

Adapter/ArrayAdapter.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ public function save(CacheItemInterface $item)
190190

191191
$now = microtime(true);
192192

193-
if (0 === $expiry) {
194-
$expiry = \PHP_INT_MAX;
195-
}
196-
197-
if (null !== $expiry && $expiry <= $now) {
198-
$this->deleteItem($key);
193+
if (null !== $expiry) {
194+
if (!$expiry) {
195+
$expiry = \PHP_INT_MAX;
196+
} elseif ($expiry <= $now) {
197+
$this->deleteItem($key);
199198

200-
return true;
199+
return true;
200+
}
201201
}
202202
if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
203203
return false;

Adapter/ProxyAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static function (CacheItemInterface $innerItem, array $item) {
9292
$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"]];
9393
}
9494
$innerItem->set($item["\0*\0value"]);
95-
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6F', 0 === $item["\0*\0expiry"] ? \PHP_INT_MAX : $item["\0*\0expiry"])) : null);
95+
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6F', $item["\0*\0expiry"])) : null);
9696
},
9797
null,
9898
CacheItem::class
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
16+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
17+
use Symfony\Component\Cache\Adapter\RedisAdapter;
18+
use Symfony\Component\Cache\CacheItem;
19+
20+
/**
21+
* @group integration
22+
*/
23+
class ProxyAdapterAndRedisAdapterTest extends AbstractRedisAdapterTest
24+
{
25+
protected $skippedTests = [
26+
'testPrune' => 'RedisAdapter does not implement PruneableInterface.',
27+
];
28+
29+
public static function setUpBeforeClass(): void
30+
{
31+
parent::setUpBeforeClass();
32+
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
33+
}
34+
35+
public function createCachePool($defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
36+
{
37+
return new ProxyAdapter(new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), 100), 'ProxyNS', $defaultLifetime);
38+
}
39+
40+
public function testSaveItemPermanently()
41+
{
42+
$setCacheItemExpiry = \Closure::bind(
43+
static function (CacheItem $item, $expiry) {
44+
$item->expiry = $expiry;
45+
46+
return $item;
47+
},
48+
null,
49+
CacheItem::class
50+
);
51+
52+
$cache = $this->createCachePool(1);
53+
$value = rand();
54+
$item = $cache->getItem('foo');
55+
$setCacheItemExpiry($item, 0);
56+
$cache->save($item->set($value));
57+
$item = $cache->getItem('bar');
58+
$setCacheItemExpiry($item, 0.0);
59+
$cache->save($item->set($value));
60+
$item = $cache->getItem('baz');
61+
$cache->save($item->set($value));
62+
63+
$this->assertSame($value, $this->cache->getItem('foo')->get());
64+
$this->assertSame($value, $this->cache->getItem('bar')->get());
65+
$this->assertSame($value, $this->cache->getItem('baz')->get());
66+
67+
sleep(1);
68+
$this->assertSame($value, $this->cache->getItem('foo')->get());
69+
$this->assertSame($value, $this->cache->getItem('bar')->get());
70+
$this->assertFalse($this->cache->getItem('baz')->isHit());
71+
}
72+
}

Tests/Adapter/TagAwareAdapterTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use Psr\Cache\CacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
17-
use Psr\Log\LoggerInterface;
1817
use Symfony\Component\Cache\Adapter\AdapterInterface;
1918
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2019
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2120
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
22-
use Symfony\Component\Cache\LockRegistry;
2321
use Symfony\Component\Cache\Tests\Fixtures\PrunableAdapter;
2422
use Symfony\Component\Filesystem\Filesystem;
2523

@@ -199,24 +197,6 @@ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags
199197
$this->assertFalse($item->isHit());
200198
}
201199

202-
public function testLog()
203-
{
204-
$lockFiles = LockRegistry::setFiles([__FILE__]);
205-
206-
$logger = $this->createMock(LoggerInterface::class);
207-
$logger
208-
->expects($this->atLeastOnce())
209-
->method($this->anything());
210-
211-
$cache = new TagAwareAdapter(new ArrayAdapter());
212-
$cache->setLogger($logger);
213-
214-
// Computing will produce at least one log
215-
$cache->get('foo', static function (): string { return 'ccc'; });
216-
217-
LockRegistry::setFiles($lockFiles);
218-
}
219-
220200
/**
221201
* @return MockObject&PruneableCacheInterface
222202
*/

Traits/ContractsTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait ContractsTrait
3131
doGet as private contractsGet;
3232
}
3333

34-
private $callbackWrapper = [LockRegistry::class, 'compute'];
34+
private $callbackWrapper;
3535
private $computing = [];
3636

3737
/**
@@ -41,8 +41,16 @@ trait ContractsTrait
4141
*/
4242
public function setCallbackWrapper(?callable $callbackWrapper): callable
4343
{
44+
if (!isset($this->callbackWrapper)) {
45+
$this->callbackWrapper = \Closure::fromCallable([LockRegistry::class, 'compute']);
46+
47+
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
48+
$this->setCallbackWrapper(null);
49+
}
50+
}
51+
4452
$previousWrapper = $this->callbackWrapper;
45-
$this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
53+
$this->callbackWrapper = $callbackWrapper ?? static function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
4654
return $callback($item, $save);
4755
};
4856

@@ -82,6 +90,10 @@ static function (CacheItem $item, float $startTime, ?array &$metadata) {
8290
$this->computing[$key] = $key;
8391
$startTime = microtime(true);
8492

93+
if (!isset($this->callbackWrapper)) {
94+
$this->setCallbackWrapper($this->setCallbackWrapper(null));
95+
}
96+
8597
try {
8698
$value = ($this->callbackWrapper)($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
8799
$setMetadata($item, $startTime, $metadata);

0 commit comments

Comments
 (0)
0