修改 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' => 'complex',
'default' => [
'type' => 'file',
// 全局缓存有效期(0为永久有效)
'expire'=> 0,
// 缓存前缀
'prefix'=> 'think',
// 缓存目录
'path' => '../runtime/cache/',
],
'redis' => [
'type' => 'redis',
'host' => '39.xx4.xxx.xxx',
'port' => 6379,
'password' => 'xxxxxxxx',
// 全局缓存有效期(0为永久有效)
// 'expire'=> 0,
// 缓存前缀
'prefix'=> 'think:',
'timeout'=> 3600
],
// 添加更多的缓存类型设置
];
利用 composer 安装 predis/predis 依赖
composer require predis/predis

使用示例
public function redis(){
dump(Cache::store('redis')->set('sfdsf','yingying',1000000));
}

本文介绍如何在ThinkPHP框架中配置多种缓存类型,包括文件缓存和Redis缓存。通过修改config/cache.php文件,实现复合缓存机制,并详细展示了Redis缓存的配置参数,如主机、端口、密码等。此外,还提供了使用Redis缓存的示例代码。
1万+

被折叠的 条评论
为什么被折叠?



