TP6框架中Redis操作服务类的示例分析

小编给大家分享一下TP6框架中redis操作服务类的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站建设、通州网络推广、微信小程序开发、通州网络营销、通州企业策划、通州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供通州建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

1. 定义服务类

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

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);

以上是“TP6框架中Redis操作服务类的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网站名称:TP6框架中Redis操作服务类的示例分析
网页网址:http://bzwzjz.com/article/psodgh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设公司 温江网站设计 网站制作 宜宾网站设计 成都网站建设公司 成都网站制作 定制网站制作 定制级高端网站建设 网站制作 网站建设方案 自适应网站设计 企业网站设计 成都网站建设 盐亭网站设计 四川成都网站设计 成都做网站建设公司 成都网站设计 网站制作报价 做网站设计 成都企业网站建设公司 成都定制网站建设 成都网站建设公司