tp6配置redis

一、redis的基础配置:

1、找到config配置文件下的cache.php

2、将redis配置代码加进去即可

<?php

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

return [
    // 默认缓存驱动
    'default' => env('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',
        ],
    ],
];

3、调用:

<?php
namespace app\pyp\controller;


use think\facade\Cache;


class Ceshi
{
    public function index()
    {
        return '欢迎来到pyp世界';
    }

    public function redisCeshi()
    {
        Cache::store('redis')->set('name','value',3600);
        return '操作成功';
    }
}

二、redis库的切换

1、在公共文件夹下 app/common (自建的,放哪儿按个人习惯)  新建Redis.php文件

2、键入代码

<?php
namespace app\common;


use think\facade\Cache;

class Redis
{
    /**
     * redis 切库
     * @param int $num 库号(0~15)
     */
    public static function select($num=0)
    {
        Cache::store('redis')->handler()->select($num);
    }

}

3、执行调用

<?php
namespace app\pyp\controller;

use app\common\Redis;
use think\facade\Cache;

class Ceshi
{
    public function index()
    {
        return '欢迎来到pyp世界';
    }

    public function redisCeshi()
    {
        Redis::select(7);
        Cache::store('redis')->set('name8','chenxi',3600);
        $name8 = Cache::store('redis')->get('name8');
        return $name8;
    }
}

4、访问可返回值  chenxi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值