andre.kolell's avatar

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\";"
0 likes
4 replies
mynet's avatar

Hi Andre;

I have the same issue in my Laravel 5.1 based system.

I am flushing the whole Redis DB when system is in low traffic (after midnight), but this is not a good solution.

Have you found any solution to this problem?

robdesilets's avatar

Hi All, I hit this issue today -- the data is expiring and being removed but the tags themselves seem to stay around indefinitely. Did anyone find a solution to this? Thanks!

AlexanderWright's avatar

Good morning all.

I'm also looking for a solution to this issue ☺️

Apologies for waking the thread.

lcdev's avatar

Hi, did you found any solution?

Please or to participate in this conversation.