一 、//連接redis
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");
}
}