byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.
27,438 14
Install
composer require byerikas/cache-tags
Latest Version:1.0.8
License:MIT
Last Updated:Jun 21, 2026
Links: GitHub  ·  Packagist
Maintainer: byErikas

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag. This is done by adding a new store driver, redis-tags, that adds a different functionality. Laravel 10+ changed how cache logic works, removing the ability to retrieve an item if not using the exact tags that were present when setting a cache key. E.g.:

Cache::tags(["tag1", "tag2"])->put("key", true);
Cache::tags(["tag1"])->get("key"); //Will result in the default value (null)
Cache::tags(["tag1", "tag2"])->get("key"); //Will result in true
Cache::tags(["tag2", "tag1"])->get("key"); //Will result in the default value (null)

This changes how that works. Tags no longer have an impact on keys. Tags are used strictly for tagging, and not for creating different key namespaces. E.g:

Cache::tags(["tag1", "tag2"])->put("key", true);
Cache::tags(["tag1"])->get("key"); //Will result in true
Cache::tags(["tag2", "tag1"])->get("key"); //Will result in true
Cache::get("key"); //Will result in true

Using Cache::forever() will now store items for 100 days, not forever, to allow the values to be memory managed, instead of tags. Flushing tags - one is enough to flush the value out of the cache. This leaves some empty references in tag sets but is mitigated by the stale tag pruning command. (see Installation)

Cache::tags(["tag1", "tag2"])->put("key", true);
Cache::tags(["tag1"])->flush(); //Will flush "key"
Cache::flush(); //Will flush "key"

Limitations

Different tags DON'T equal different key namespaces. Tagged and non-tagged items use the same key sequence. Ensure keys are unique - tags only tag, not alter keys. E.g.:

Cache::tags(["tag1", "tag2"])->put("key", "value1");
/** This overwrites the key above since there is a shared tag. */
Cache::tags(["tag2"])->put("key", "value2");
Cache::tags(["tag1"])->get("key"); //Will result in "value2"
Cache::get("key"); //Will result in "value2"

Installation

Please read the Limitations section before use as this can have breaking changes. The package can be installed using:

composer require byerikas/cache-tags

To use the new driver - edit your config/cache.php and under stores.YOUR_STORE.driver set the value to redis-tags, and run php artisan optimize. It's recommended to have a scheduled command that would prune your stale tags to clean up memory. The command is php artisan cache:prune-stale-tags, and should come with Laravel out of the box.

To prevent any memory issues use Redis/Valkey maxmemory-policy of volatile-*.

Related Packages

mochaka/redcard

Autocomplete implementation using PHP and Redis

1,491 2
oneforge/lada-cache

A Redis based, automated and scalable database caching layer for Laravel 5.1+

3 0
xtcat/laravel-redis-fallback

Laravel 5 redis cache fallback to file

23,721 5
spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

464,910 591
victorsigma/redcard

Autocomplete implementation using PHP and Redis

124 3