ThinkPHP6中Redis操作服务类的记录

1. 定义服务类

<?php

declare (strict_types=1);

namespace app\api\service\common;

use think\facade\Cache;

/**
 * 缓存服务
 * Class RedisService
 *
 * @package app\api\service\common
 */
class RedisService
{
    private $expire;
    private $expire_at;

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

    /**
     * 处理缓存key(添加前缀...)
     *
     * @param string $key  key
     *
     * @return string
     */
    private function cacheKey(string $key): string
    {
        return Cache::getCacheKey($key);
    }

    /**
     * 缓存程序运行结果
     *
     * @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);
    }

    /**
     * 程序运行锁
     * @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];
    }

    /**
     * 设置有效时间
     *
     * @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;
    }

    /**
     * 设置到期时间
     *
     * @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;
    }

    /**
     * 调用原生redis方法
     *
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        $cache_key = $arguments[0] = $this->cacheKey($arguments[0]);

        $result = $this->client()->{$name}(...$arguments);
        // 设置过期时间
        $this->expire && $this->client()->expire($cache_key, $this->expire);
        $this->expire_at && $this->client()->expireAt($cache_key, $this->expire_at);

        return $result;
    }
}

2. 定义门面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. 如何使用

3.1 程序锁

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 '操作频繁,请稍后再试';
        return $result['data']; // 成功返回数据 3
    }

3.2 方法数据缓存

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

    return $result; // 成功返回数据 3,有效时长5秒
}

3.3 简化过期时间设置

// 24小时到期
Redis::setExpire(86400)->hSet('expire:demo', 'hash-key', 'hash-value');
// 2021-08-24 23:59:59到期
Redis::setExpireAt(strtotime('2021-08-24 23:59:59'))->hSet('expireAt:demo', 'hash-key', 'hash-value');

3.4 普通调用

// 普通调用,直接跟redis方法名
Redis::set('set:demo', 132456);
// idea支持代码提示调用
Redis::client()->set('set:demo', 132456);
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 2

lock代码完全错的。 拿锁应该nx来拿 释放就del锁

2年前 评论
Colorado (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!