一、數據緩存的增刪改查
字面理解:從數據庫中獲取的數據緩存起來,緩存到硬盤?內存?數據庫?…
緩存在哪裏,配置在basic中的web.php
如何使用
public function actionIndex(){
//獲取緩存組件
$cache = \Yii::$app->cache;
//往緩存當中存數據
$cache->add('key1','hello moto');
$cache->add('key1','hello 123');不覆蓋,不執行
當改方法被執行,則'key1' => 'hello moto'就被寫入到緩存當中
//從緩存當中讀取數據
$data = $cache->get('key1');
//修改緩存數據
$cache->set('key1','hello world');
//刪除數據
$cache->delete('key1');
//清空數據
$cache->flush();
}
二、緩存數據的有效期設置
$cache->add('key1','hello moto',15);有效期15秒
$cache->set('key1','hello moto',15);有效期15秒
三、數據緩存中依賴關係的講解
//文件依賴
$aaa = new \yii\caching\FileDependency('fileName' => 'xx.txt');
$cache->add('key1','hello moto',100,$aaa);
->表示,key1與xx.txt關聯,當xx.txt發生更改,則$cache失效
//表達式依賴
$aaa = new \yii\caching\ExpressionDependency(
['expression' => '\Yii::$app->request->get['name']']
);
$cache->add('key1','hello moto',100,$aaa);
->表示,key1餘表達式expression關聯,當表達式的值發生改變,緩存失效
//DB依賴
$aaa = new \yii\caching\DBDependency(
['sql' => 'select * from yii.order']
);
$cache->add('key1','hello moto',100,$aaa);
->表示,key1與sql關聯,當sql語句結果發生變化,則$cache無效