ThinkPHP6使用Redis

ThinkPHP6使用Redis

我的运行环境
CentOS 8.2+宝塔

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

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

  1. 在TP6项目设置Redis参数配置
    config/cache.php
<?php

use think\facade\Env;

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

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

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

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

使用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('cache.stores.redis'));

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

        dd($pasa);
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值