PHP code example of chalcedonyt / laravel-redis-tagger

1. Go to this page and download the library: Download chalcedonyt/laravel-redis-tagger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chalcedonyt / laravel-redis-tagger example snippets


Chalcedonyt\RedisTagger\Providers\RedisTaggerServiceProvider::class

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

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

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

class PostCountToday extends PostCount
{
    public function __construct(){
        parent::__construct();
        $this -> tags[]= 'today';
    }    
}

class PostCountYesterday extends PostCount
{
    public function __construct(){
        parent::__construct();
        $this -> tags[]= 'yesterday';
    }    
}

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

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

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