lundi 2 octobre 2017

Tagged cache items with TTL mess up Redis cache in Laravel

When I use cache tags on items with a TTL in Laravel the cache gets filled up as the references from the tag are not (automatically) removed when the cached item expires. See the example below which I ran on a fresh Laravel 5.5 installation with Redis configured as the cache.

How do I avoid that the cache gets messed up when using tags with items that have a TTL?

Store an item tagged with „people“ with a TTL of 1 minute:

vagrant@debian:~/laravel-redis-tag-testing$ php artisan tinker
Psy Shell v0.8.11 (PHP 7.0.23-1~dotdeb+8.1 — cli) by Justin Hileman
>>> Cache::tags(['people'])->put('John', 'Some data about John', 1);
=> null

The item is the only element that is being stored with a TTL:

vagrant@debian:~/laravel-redis-tag-testing$ redis-cli -n 1
127.0.0.1:6379[1]> KEYS *
1) "laravel:59d275f24aa60438267011:standard_ref"
2) "laravel:tag:people:key"
3) "laravel:c041fadf49537a608ebcdae3135b1baed8bb55fe:John"
127.0.0.1:6379[1]> TTL "laravel:59d275f24aa60438267011:standard_ref"
(integer) -1
127.0.0.1:6379[1]> TTL "laravel:tag:people:key"
(integer) -1
127.0.0.1:6379[1]> TTL "laravel:c041fadf49537a608ebcdae3135b1baed8bb55fe:John"
(integer) 32

After the item expired it is gone but the tag itself and the reference are still there:

127.0.0.1:6379[1]> KEYS *
1) "laravel:59d275f24aa60438267011:standard_ref"
2) "laravel:tag:people:key"
127.0.0.1:6379[1]> SMEMBERS "laravel:59d275f24aa60438267011:standard_ref"
1) "laravel:c041fadf49537a608ebcdae3135b1baed8bb55fe:John"
127.0.0.1:6379[1]> GET "laravel:tag:people:key"
"s:22:\"59d275f24aa60438267011\";"



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire