Redis operation service class record in thinkphp6

Time:2021-12-2

1. Define service class

<?php

declare (strict_types=1);

namespace app\api\service\common;

use think\facade\Cache;

/**
 *Cache service
 * Class RedisService
 *
 * @package app\api\service\common
 */
class RedisService
{
    private $expire;
    private $expire_at;

    /**
     *Get redis handle
     *
     * @return object|null
     */
    public function client(): ?object
    {
        return Cache::store('redis')->handler();
    }

    /**
     *Process cache key (add prefix...)
     *
     * @param string $key  key
     *
     * @return string
     */
    private function cacheKey(string $key): string
    {
        return Cache::getCacheKey($key);
    }

    /**
     *Cache program running results
     *
     * @param          $key
     * @param callable $callback
     * @param int      $expire
     *
     * @return mixed
     */
    public function cache($key, callable $callback, int $expire = 3600)
    {
        $cache_key = $this->cacheKey($key);
        $cache = $this->client()->get($cache_key);
        if (! $cache || ! unserialize($cache)) {
            $data = $callback();
            $this->client()->set($cache_key, $cache = serialize($data), $expire);
        }

        return unserialize($cache);
    }

    /**
     *Program run lock
     * @param          $key
     * @param callable $callback
     * @param int      $timeout
     *
     * @return array
     */
    public function lock($key, callable $callback, int $timeout = 10): array
    {
        $key = $this->cacheKey($key);
        if (! $this->client()->setnx($key, 1)) return ['code' => 0, 'data' => null];
        $this->client()->expire($key, $timeout);
        $data = $callback();
        $this->client()->del($key);

        return ['code' => 1, 'data' => $data];
    }

    /**
     *Set effective time
     *
     * @param $ttl
     *
     * @return $this|false
     */
    public function setExpire($ttl)
    {
        if ($this->expire_at) throw new \Exception('setExpire and setExpireAt can not set both');
        $this->expire = $ttl;

        return $this;
    }

    /**
     *Set expiration time
     *
     * @param $timestamp
     *
     * @return $this|false
     */
    public function setExpireAt($timestamp)
    {
        if ($this->expire > 0) throw new \Exception('setExpire and setExpireAt can not set both');
        $this->expire_at = $timestamp;

        return $this;
    }

    /**
     *Call the native redis method
     *
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        $cache_key = $arguments[0] = $this->cacheKey($arguments[0]);

        $result = $this->client()->{$name}(...$arguments);
        //Set expiration time
        $this->expire && $this->client()->expire($cache_key, $this->expire);
        $this->expire_at && $this->client()->expireAt($cache_key, $this->expire_at);

        return $result;
    }
}

2. Define the facade

<?php


namespace app\api\facade;


use app\api\service\common\RedisService;
use think\Facade;

/**
 * Class Redis
 *
 * @package app\api\facade
 *
 * @method static \Redis client()
 * @method static \Redis setExpire($ttl)
 * @method static \Redis setExpireAt($timestamp)
 * @method static mixed cache($key, callable $callback, int $expire = 3600)
 * @method static array lock($key, callable $callback, int $timeout = 10)
 */
class Redis extends Facade
{
    protected static function getFacadeClass()
    {
        return RedisService::class;
    }
}

3. How to use

3.1 program lock

public function test()
    {
        $a = 1;
        $b = 2;
        $result = Redis::lock('lock:demo', function () use ($a, $b) {
            return $a + $b;
        }, 5);
        If ($result ['code '] = = 0) return' operation is frequent, please try again later ';
        return $result['data']; //  Successfully returned data 3
    }

3.2 method data cache

public function test()
{
    $a = 1;
    $b = 2;
    $result = Redis::cache('cache:demo', function () use ($a, $b) {
        return $a + $b;
    }, 5);

    return $result; //  Successfully returned data 3, valid for 5 seconds
}

3.3 simplify expiration time settings

//Due 24 hours
Redis::setExpire(86400)->hSet('expire:demo', 'hash-key', 'hash-value');
//Due at 23:59:59 on August 24, 2021
Redis::setExpireAt(strtotime('2021-08-24 23:59:59'))->hSet('expireAt:demo', 'hash-key', 'hash-value');

3.4 general call

//Normal call, directly followed by redis method name
Redis::set('set:demo', 132456);
//Idea supports code prompt calling
Redis::client()->set('set:demo', 132456);

This work adoptsCC agreement, reprint must indicate the author and the link to this article