一、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