【TP5 :資料庫:查詢構造器:鏈式操作】cache
阿新 • • 發佈:2019-01-31
cache方法用於查詢快取操作,連貫操作方法
用於select
、find
、value
和column
方法,以及其衍生方法
在快取有效期之內不會再次進行資料庫查詢操作,而是直接獲取快取中的資料
//find方法使用cache方法
Db::table('think_user')->where('id=5')->cache(true)->find();
第一次查詢結果會被快取,第二次查詢相同的資料的時候就會直接返回快取中的內容,而不需要再次進行資料庫查詢操作。
快取有效期
預設由快取配置引數決定,可指定
Db::table('think_user')-> cache(true,60)->find();
// 或者使用下面的方式 是等效的
Db::table('think_user')->cache(60)->find();
//表示對查詢結果的快取有效期60秒。
快取標識
$result = Db::table('think_user')->cache('key',60)->find();
$data = \think\Cache::get('key');
外部就可以通過 \think\Cache
類直接獲取查詢快取的資料,使查詢快取更有效率
快取標籤
Db::table('think_user')-> cache('key',60,'tagName')->find();
快取更新或清除
指定快取標識能讓update
,delete
操作更新
,清除
快取
Db::table('think_user')->cache('user_data')->select([1,3,5]);
Db::table('think_user')->cache('user_data')->update(['id'=>1,'name'=>'thinkphp']); //更新快取
或
Db::table('think_user')->cache('user_data') ->delete(['id'=>1); //清除快取
使用find
方法且主鍵查詢,不需指定快取標識,會自動清理快取
Db::table('think_user')->cache(true)->find(1);
Db::table('think_user')->update(['id'=>1,'name'=>'thinkphp']);
Db::table('think_user')->cache(true)->find(1);
//最後查詢的資料會是更新後的資料。