数据缓存中的依赖关系

1、缓存文件依赖

1  $cache = \YII::$app->cache;
2         $dependency = new \yii\caching\FileDependency(['fileName'=>'hw.txt']);
3         $cache->add('file_key','hello world',3000,$dependency);
4         var_dump($cache->get('file_key'));

当hw.txt文件中的内容出现变化,则该缓存过期

2、表达式依赖:

1 $cache = \YII::$app->cache;
2         $dependency = new \yii\caching\ExpressionDependency(['expression'=>'\YII::$app->request->get("name")']);
3         $cache->add('expression_key','hello world',3000,$dependency);
4         var_dump($cache->get('expression_key'));

当 ['expression'=>'\YII::$app->request->get("name")'] 中获得的name的值发生了变化,则缓存过期

3、DB依赖

1 $cache=\YII::$app->cache;
2         $dependency = new \yii\caching\DbDependency(
3             ['sql'=>'SELECT count(*) FROM yii.order']
4         );
5         $cache->add('db_key','hello_world',3000,$dependency);
6         var_dump($cache->get('db_key'));

 

posted @ 2015-08-26 15:11  todaytoday  阅读(196)  评论(0编辑  收藏  举报