chalcedonyt / laravel-redis-tagger
A Helper function for Redis in Laravel to organize tags.
Installs: 1 217
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: >=5.3.0
- illuminate/support: ~5.1
- predis/predis: ~1.0
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is not auto-updated.
Last update: 2024-11-13 11:36:18 UTC
README
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.