Home > PHP Framework > ThinkPHP > body text

Let's talk about how to use Redis in ThinkPHP6

青灯夜游
Release: 2022-08-18 11:41:01
forward
2666 people have browsed it

How to use Redis in ThinkPHP6? The following article will introduce how to use Redis in ThinkPHP6. I hope it will be helpful to everyone!

Let's talk about how to use Redis in ThinkPHP6

My running environment: CentOS 8.2 Pagoda

If the environment is different, please install Redis and php extensions according to your own environment

First install Redis in Pagoda [Software Store], and then install the Redis extension in the corresponding PHP version management

1. Set the Redis parameter configuration in the TP6 project

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;,//如果没有设置密码为空
        ],
        // 更多的缓存连接
    ],
];
Copy after login

2. Using 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(&#39;pasawu&#39;, &#39;test&#39;);
        $pasa = $redis->get(&#39;pasawu&#39;);

        dd($pasa);
    }
}
Copy after login

[Related tutorial recommendations: thinkphp framework]

The above is the detailed content of Let's talk about how to use Redis in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!