use think\Cache;
转自:http://www.zzuyxg.top/article/444.html
转自:https://blog.csdn.net/qq_37462176/article/details/79408918
thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作。支持的缓存类型包括file、memcache、wincache、sqlite、redis和xcache等,默认情况下是file类型,配置redis缓存可以单一配置redis也可以同时使用多个缓存类型。配置方式分别如下:
一、仅配置redis缓存,在配置文件(app/config.php)中修改缓存设置如下:
二、配置多个缓存类型,使用符合缓存类型,配置方式如下:
'cache' => [ // 使用复合缓存类型 'type' => 'complex', // 默认使用的缓存 'default' => [ // 驱动方式 'type' => 'File', // 缓存保存目录 'path' => CACHE_PATH, ], // 文件缓存 'file' => [ // 驱动方式 'type' => 'file', // 设置不同的缓存保存目录 'path' => RUNTIME_PATH . 'file/', ], // redis缓存 'redis' => [ // 驱动方式 'type' => 'redis', // 服务器地址 'host' => '192.168.1.100', ], ],
使用符合缓存类型时,需要根据需要使用store方法切换缓存。
当使用
Cache::set('name', 'value'); Cache::get('name');
的时候,使用的是default缓存标识的缓存配置。如果需要切换到其它的缓存标识操作,可以使用:
// 切换到file操作 Cache::store('file')->set('name','value'); Cache::get('name'); // 切换到redis操作 Cache::store('redis')->set('name','value'); Cache::get('name');
比如,查询一篇文章时首先从redis中查询,若未查到信息则从数据库中查询出结果,并存储到redis中。
Redis的CURD操作
Thinkphp5怎么扩展Redis数据库,实现Redis的CURD操作
Redis怎么使用Redis数据库,本篇文章主要介绍在Thinkphp5项目中如何使用Redis数据库
一、基础环境
PHP扩展: http://www.zhaisui.com/article/38.html
二、数据库配置
1、头部引用Redis类
use think\cache\Driver;
2、Redis 数据库配置信息
$config = [ 'host' => '服务器IP地址', 'port' => Redis端口号, 'password' => 'Redis访问密码', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', ];
三、 Redis基本使用
$Redis=new Redis($config); $Redis->set("test","test"); echo $Redis->get("test");
四、具体代码如下:
namespace app\index\controller; use \think\Db; use think\cache\driver\Redis; class Index { public function index() { $config = [ 'host' => '服务器IP地址', 'port' => Redis端口号, 'password' => 'Redis访问密码', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', ]; $Redis=new Redis($config); $Redis->set("test","test"); echo $Redis->get("test"); } }