tp6配置
config/cache.php
<?php
// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
return [
// 默认缓存驱动
'default' => env('cache.driver', 'redis'),
// 缓存连接方式配置
'stores' => [
'file' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => '',
// 缓存前缀
'prefix' => '',
// 缓存有效期 0表示永久缓存
'expire' => 0,
// 缓存标签前缀
'tag_prefix' => 'tag:',
// 序列化机制 例如 ['serialize', 'unserialize']
'serialize' => [],
],
'redis' => [
// 驱动方式
'type' => 'redis',
// 缓存主机
'host' => '127.0.0.1',
// 缓存端口
'port' => '6379',
// 缓存密码
'password' => '',
],
],
];
tp6使用缓存时要引入 use think\facade\Cache;
tp5.1配置
config/cache.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
return [
// 驱动方式
'type' => 'redis',
// 缓存主机
'host' => '127.0.0.1',
// 缓存端口
'port' => '6379',
// 缓存密码
'password' => '',
];
使用时引入 use think\facade\Cache;
tp5配置
application/config.php
// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
'cache' => [
// 驱动方式
'type' => 'redis',
// 缓存主机
'host' => '127.0.0.1',
// 缓存端口
'port' => '6379',
// 缓存密码
'password' => '',
],
使用时引入 use think\Cache;
常用方法
public function redis()
{
Cache::set('name', 'value', 3600);
$sj = Cache::get('name');
return $sj;
}
// 设置缓存数据
cache('name', $value, 3600);
// 获取缓存数据
var_dump(cache('name'));
Db::table('think_user')->cache(60)->find();