chalcedonyt/laravel-redis-tagger

用于Laravel中组织标签的Redis辅助函数。

0.4 2015-10-15 16:25 UTC

This package is not auto-updated.

Last update: 2024-09-18 10:42:53 UTC


README

一个辅助门面和函数,用于组织Redis标签,实现简单的类型提示、内置参数验证和占位符替换。

安装

通过Composer(必须设置为开发版稳定性)

$ composer require chalcedonyt/laravel-redis-tagger

将提供者和门面包含到app.php中。

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

用法 - GET/SET

php artisan redis_tagger:make UserPosts\\PostCount

您只需要设置tags值。您可以插入一个普通字符串、一个{tagtemplate}或一个返回值的Closure(闭包)。(这允许类型提示)。

在调用时必须定义任何{tagtemplate}键。

class PostCount extends Tagger
{
    public function __construct(){
        parent::__construct();
        $this -> tags = [
            'user_posts',
            '{type}',
            '{post_id}' => function( Post $post ){
                return $post -> id;
            },
            'count'
        ];
    }
}

对于键值操作,您可以在RedisTagger门面上调用::set或::get。

$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.

同样,您可以使用以下方式检索值

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

您可能只需要返回一个键(例如用于集合)

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

您也可以通过向父级的$tags变量中添加内容来扩展您创建的任何标签器。

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.

用法 - KEYS

RedisTagger还封装了Redis的::keys函数。在调用keys时,不会对参数进行验证。任何缺少的{tagtemplates}都将转换为*

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

用法 - 提取标签值

您可以使用::valueOfTagInKey函数从键中提取{tag}的值。

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

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

变更日志

请参阅CHANGELOG以获取更多最近更改的信息。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件