• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > 分享TP6框架中Redis操作服务类的记录

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

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了TP6框架,Redis操作服务类等相关知识,希望对您有所帮助

下面给大家分享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 = $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

<?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. 如何使用

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法
  • PhotoShop图层样式制作透明质感玻璃文字效果教程
  • 深入浅析vue3+vite中怎么使用svg图标
  • 遇到的uni-app的坑(uni-easyinput清空值,datetimerange置空)
  • 看看使用uni-app如何编写一个五子棋小游戏
  • wordpress的login.php打不开怎么办
  • css中实现背景透明的三种方式
  • 为什么不建议使用@import引入css
  • 浅析网页与小程序间怎么进行通信
  • 了解优化PHP7性能的几个设置

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • DEDECMS的优化方案
    • 判断是否为jquery对象使用什么运算符
    • 带你搞懂uniapp跨域问题(实例详解)
    • DEDECMS注册成功页面加入马上激活链接转向注册邮
    • Javascript如何获取用户输入的值
    • 关于ThinkPhp view路径用到的常量 __STATIC__ __JS__ __CSS__等
    • thinkphp5报错显示nginx 404页面的解决办法
    • Thinkphp6如何利用ZipArchive打包下载文件
    • 值得掌握一下支持Laravel 9的Aliyun OSS Storage扩展
    • 提高网站排名的三个方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有