Redis.php
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace common\components\pagecache;
use Yii;
use common\components\cachekey\Parser;
/**
* @author wuxiao
* @date 2016-8-23
*/
class Redis extends \yii\redis\Cache
{
public function init()
{
if (isset(Yii::$app->params['enableCache']) && !Yii::$app->params['enableCache'])
{
Yii::$app->db->enableQueryCache = false;
Yii::$app->db->enableSchemaCache = false;
}
parent::init();
}
public function buildKey($key)
{
if ($key instanceof Parser){
$key = $key->key();
}
return parent::buildKey($key);
}
/**
* @inheritdoc
*/
protected function getValue($key)
{
if (isset(Yii::$app->params['enableCache']) && !Yii::$app->params['enableCache'])
{
Yii::$app->response->headers->set('X-PageCache', 'DISABLE');
return false;
}
$result = parent::getValue($key);
if ($result){
Yii::$app->response->headers->add('X-PageCache', 'HIT');
}else{
Yii::$app->response->headers->add('X-PageCache', 'MISS');
}
return $result;
}
/**
* @inheritdoc
*/
protected function setValue($key, $value, $expire)
{
if (isset(Yii::$app->params['enableCache']) && !Yii::$app->params['enableCache'])
{
Yii::$app->response->headers->set('X-PageCache', 'DISABLE');
return true;
}
Yii::$app->response->headers->add('X-PageCache', 'SAVE');
return parent::setValue($key, $value, $expire);
}
}