A Helper function for Redis in Laravel to organize tags.

0.4 2015-10-15 16:25 UTC

This package is not auto-updated.

Last update: 2024-04-03 08:31:12 UTC


A helper Facade and functions to organize Redis tags, enabling easy type-hinting, built-in parameter validation, and placeholder replacement.


Via Composer (minimum stability must be set to dev)

$ composer require chalcedonyt/laravel-redis-tagger

Include the Provider and Facade into app.php.

'RedisTagger' => Chalcedonyt\RedisTagger\Facades\RedisTagger::class

Usage - GET/SET

php artisan redis_tagger:make UserPosts\\PostCount

The only thing you need to set is the tags value. You may insert either a plain string, a {tagtemplate}, or a {tagtemplate} with a \Closure that returns a value. (This allows type-hinting).

Any {tagtemplate} keys must be defined when called.

class PostCount extends Tagger
    public function __construct(){
        $this -> tags = [
            '{post_id}' => function( Post $post ){
                return $post -> id;

For key-value operations, you may then call ::set or ::get on the RedisTagger Facade:

$post = new Post();
$post -> id = 123
$args = ['type' => 'article', 'post_id' => $post ];
RedisTagger::set('UserPosts\\PostCount', $args, 1000); //sets the key "user_posts:article:123:count" to 1000.

Likewise, you can retrieve the value with

RedisTagger::get('UserPosts\\PostCount', $args);

You may return only a key (e.g. for use with sets)

RedisTagger::getKey('UserPosts\\PostCount', $args); //returns "user_posts:article:123:count"

It is also possible to extend any taggers you create by adding to the parent's $tags variable.

class PostCountToday extends PostCount
    public function __construct(){
        $this -> tags[]= 'today';
class PostCountYesterday extends PostCount
    public function __construct(){
        $this -> tags[]= 'yesterday';
RedisTagger::set('UserPosts\PostCountToday', $args, 1000); //sets the key "user_posts:article:123:count:today" to 1000.
RedisTagger::set('UserPosts\PostCountYesterday', $args, 1000); //sets the key "user_posts:article:123:count:yesterday" to 1000.

Usage - KEYS

RedisTagger also wraps the ::keys function of Redis. When calling keys, no validation is done on the arguments. Any missing {tagtemplates} will be cast to *:

$args = ['type' => 'a??'];
RedisTagger::keys('UserPosts\\PostCount', $args); //returns "user_posts:a??:*:count"

Usage - Extracting tag values

You may extract the value of a {tag} from a key by using the ::valueOfTagInKey function.

$key = "user_posts:article:123:count:yesterday";

RedisTagger::valueOfTagInKey('UserPosts\\PostCount', $key, 'post_id'); //returns "123"

Change log

Please see CHANGELOG for more information what has changed recently.


The MIT License (MIT). Please see License File for more information.