Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ChainAdapter uses default lifetime instead of ttl when saving in get() function #59856

Unanswered
magicW asked this question in Q&A
Discussion options

The Used Symfony-Cache-Version is 5.4.45. I have a ChainAdapter wit a default lifetime of 86400:

chain_cache:
 default_lifetime: 86400
 adapters:
 - cache.adapter.apcu
 - cache.adapter.redis

When i save an cacheItem with a special ttl there is no problem. But when I have a second VM without an value in the apcu, the chainAdapter will get the value from Redis and then save it to the chain Adapter. The Problem ist that in this case there won't be used the ttl, nore the default lifetime. Here an example to reproduce:

public function test( ChainAdapter $cache ) {
 $cacheKey = 'chainTest';
 $cacheItem = $cache->getItem($cacheKey);
 $cacheItem->set('myValue');
 $cacheItem->expiresAfter(60);
 $cache->save($cacheItem);
 foreach (apcu_cache_info()['cache_list'] as $item) {
 if (str_contains($item['info'], $cacheKey)) {
 dump($item);
 }
 }
 apcu_clear_cache();
 $cacheItem = $cache->getItem($cacheKey);
 foreach (apcu_cache_info()['cache_list'] as $item) {
 if (str_contains($item['info'], $cacheKey)) {
 dump($item);
 }
 }
}

The ttl of the apcu Cache-Entry ist 60 in the first dump and 86400 in the second one.

You must be logged in to vote

Replies: 1 comment

Comment options

I think, this is a bug. I wrote a Test and it failed in the last line. Should i post this in issues?

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
class ChainAdapterTest extends TestCase
{
 public function testCachesExpirationAfterGet()
 {
 $adapter1 = new ArrayAdapter(20);
 $adapter2 = new ArrayAdapter(30);
 $cache = new ChainAdapter([$adapter1, $adapter2], 40);
 
 $testItemKey = "key";
 $cache->save($cache->getItem($testItemKey)->set('value')->expiresAfter(2));
 $this->assertTrue($adapter1->hasItem($testItemKey));
 $this->assertTrue($adapter2->hasItem($testItemKey));
 $adapter1->clear();
 $this->assertFalse($adapter1->hasItem($testItemKey));
 $this->assertTrue($adapter2->hasItem($testItemKey));
 $cache->getItem($testItemKey)->get();
 $this->assertTrue($adapter1->hasItem('key'));
 $this->assertTrue($adapter2->hasItem('key'));
 sleep(3);
 $this->assertFalse($adapter2->hasItem('key'));
 $this->assertFalse($adapter1->hasItem('key'));
 }
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /