PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

聊聊ThinkPHP6中如何使用Redis

青灯夜游
青灯夜游 转载
2022-08-18 11:41:01 2392浏览

thinkphp6中如何使用redis?下面本篇文章就来介绍一下thinkphp6使用redis的方法,希望对大家有所帮助!

我的运行环境:CentOS 8.2+宝塔

如果环境不同请根据自己环境安装Redis和php扩展

先在宝塔【软件商店】安装Redis,然后在对应的php版本管理安装Redis扩展

1、在TP6项目设置Redis参数配置

config/cache.php

<?php

use think\facade\Env;

// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------

return [
    // 默认缓存驱动
    &#39;default&#39; => Env::get(&#39;cache.driver&#39;, &#39;file&#39;),

    // 缓存连接方式配置
    &#39;stores&#39;  => [
        &#39;file&#39;  => [
            // 驱动方式
            &#39;type&#39;       => &#39;File&#39;,
            // 缓存保存目录
            &#39;path&#39;       => &#39;&#39;,
            // 缓存前缀
            &#39;prefix&#39;     => &#39;&#39;,
            // 缓存有效期 0表示永久缓存
            &#39;expire&#39;     => 0,
            // 缓存标签前缀
            &#39;tag_prefix&#39; => &#39;tag:&#39;,
            // 序列化机制 例如 [&#39;serialize&#39;, &#39;unserialize&#39;]
            &#39;serialize&#39;  => [],
        ],
        //新增redis
        &#39;redis&#39; => [
            // 驱动方式
            &#39;type&#39;     => &#39;redis&#39;,
            // 服务器地址
            &#39;host&#39;     => &#39;127.0.0.1&#39;,

            &#39;password&#39; => &#39;&#39;,//如果没有设置密码为空
        ],
        // 更多的缓存连接
    ],
];

2、使用Redis

<?php namespace app\api\controller;

use think\cache\driver\Redis;
use think\facade\Config;

class Test
{
    public function test()
    {
        $redis = new Redis(Config::get(&#39;cache.stores.redis&#39;));

        $redis->set('pasawu', 'test');
        $pasa = $redis->get('pasawu');

        dd($pasa);
    }
}

【相关教程推荐:thinkphp框架

声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除