您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

分享TP6框架中Redis操作服务类的记录

2024/4/6 10:53:26发布26次查看
下面由thinkphp框架教程栏目给大家分享thinkphp6中redis操作服务类的记录,希望对需要的朋友有所帮助!                                
1. 定义服务类
<?phpdeclare (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 = $this->client()->get($key);        if (! $cache || ! unserialize($cache)) {            $data = $callback();            $this->client()->set($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    {        $lock = $this->client()->get($key);        if ($lock) return ['code' => 0, 'data' => null];        $this->client()->setex($key, $timeout, 1);        $data = $callback();        $this->client()->del($key);        return ['code' => 1, 'data' => $data];    }    /**     * 设置有效时间     *     * @param $ttl     *     * @return $this|false     * @throws \exception     */    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     * @throws \exception     */    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 = $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
<?phpnamespace 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);
推荐:《最新的10个thinkphp视频教程》以上就是分享tp6框架中redis操作服务类的记录的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product