• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

谈谈ThinkPHP6使用Redis

武飞扬头像
PHP中文网
帮助121

如果环境不同请根据自己环境安装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' => '',//如果没有设置密码为空
        ],
        // 更多的缓存连接
    ],
];

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

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

        dd($pasa);
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanffkga
系列文章
更多 icon
同类精品
更多 icon
继续加载