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

Skip to content

Commit 17e0167

Browse files
Nyholmnicolas-grekas
authored andcommitted
[Cache] Use correct expiry in ChainAdapter
1 parent e693b3c commit 17e0167

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifeti
7373
$item->isHit = $sourceItem->isHit;
7474
$item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;
7575

76-
if (0 < $defaultLifetime) {
76+
if (isset($item->metadata[CacheItem::METADATA_EXPIRY])) {
77+
$item->expiresAt(\DateTime::createFromFormat('U.u', $item->metadata[CacheItem::METADATA_EXPIRY]));
78+
} elseif (0 < $defaultLifetime) {
7779
$item->expiresAfter($defaultLifetime);
7880
}
7981

src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1717
use Symfony\Component\Cache\Adapter\ChainAdapter;
1818
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
19+
use Symfony\Component\Cache\CacheItem;
1920
use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
2021
use Symfony\Component\Cache\Tests\Fixtures\PrunableAdapter;
22+
use Symfony\Contracts\Cache\ItemInterface;
2123

2224
/**
2325
* @author Kévin Dunglas <[email protected]>
@@ -34,6 +36,11 @@ public function createCachePool(int $defaultLifetime = 0, string $testMethod = n
3436
return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter($defaultLifetime), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
3537
}
3638

39+
public static function tearDownAfterClass(): void
40+
{
41+
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
42+
}
43+
3744
public function testEmptyAdaptersException()
3845
{
3946
$this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
@@ -187,6 +194,48 @@ public function testMultipleCachesExpirationWhenCommonTtlIsSet()
187194
$this->assertFalse($item->isHit());
188195
}
189196

197+
public function testExpirationOnAllAdapters()
198+
{
199+
if (isset($this->skippedTests[__FUNCTION__])) {
200+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
201+
}
202+
203+
$itemValidator = function (CacheItem $item) {
204+
$refl = new \ReflectionObject($item);
205+
$propExpiry = $refl->getProperty('expiry');
206+
$propExpiry->setAccessible(true);
207+
$expiry = $propExpiry->getValue($item);
208+
$this->assertGreaterThan(10, $expiry - time(), 'Item should be saved with the given ttl, not the default for the adapter.');
209+
210+
return true;
211+
};
212+
213+
$adapter1 = $this->getMockBuilder(FilesystemAdapter::class)
214+
->setConstructorArgs(['', 2])
215+
->setMethods(['save'])
216+
->getMock();
217+
$adapter1->expects($this->once())
218+
->method('save')
219+
->with($this->callback($itemValidator))
220+
->willReturn(true);
221+
222+
$adapter2 = $this->getMockBuilder(FilesystemAdapter::class)
223+
->setConstructorArgs(['', 4])
224+
->setMethods(['save'])
225+
->getMock();
226+
$adapter2->expects($this->once())
227+
->method('save')
228+
->with($this->callback($itemValidator))
229+
->willReturn(true);
230+
231+
$cache = new ChainAdapter([$adapter1, $adapter2], 6);
232+
$cache->get('test_key', function (ItemInterface $item) {
233+
$item->expiresAfter(15);
234+
235+
return 'chain';
236+
});
237+
}
238+
190239
private function getPruneableMock(): AdapterInterface
191240
{
192241
$pruneable = $this->createMock(PrunableAdapter::class);

0 commit comments

Comments
 (0)