New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

chalcedonyt/laravel-redis-tagger

A Helper function for Redis in Laravel to organize tags.
1,218 7
Install
composer require chalcedonyt/laravel-redis-tagger
Latest Version:0.4
PHP:>=5.3.0
Laravel:5
License:MIT
Last Updated:Dec 2, 2019
Links: GitHub  ·  Packagist
Maintainer: chalcedonyt

chalcedonyt/laravel-redis-tagger

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

Install

Via Composer (minimum stability must be set to dev)

$ composer require chalcedonyt/laravel-redis-tagger

Include the Provider and Facade into app.php.

Chalcedonyt\RedisTagger\Providers\RedisTaggerServiceProvider::class
'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(){
        parent::__construct();
        $this -> tags = [
            'user_posts',
            '{type}',
            '{post_id}' => function( Post $post ){
                return $post -> id;
            },
            'count'
        ];
    }
}

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(){
        parent::__construct();
        $this -> tags[]= 'today';
    }    
}
class PostCountYesterday extends PostCount
{
    public function __construct(){
        parent::__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.

License

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

Related Packages

namoshek/laravel-redis-sentinel

An extension of Laravels Redis driver which supports connecting to a Redis maste...

1,087,628 40
mmeyer2k/redistree

A clean Redis web interface for Laravel 5

629,555 9
cfernandez777/laravel-redis-sentinel

An extension of Laravels Redis driver which supports connecting to a Redis maste...

14,798 1
um-flint/laravel-multi-tenant-redis

Laravel package to allow for a better "cache:clear".

557 0
vetruvet/laravel-phpredis

Use phpredis as the redis connection in Laravel

130,833 43

Version History

Version Released PHP Laravel License
0.4 >=5.3.0 ~5.1 MIT