1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\caching; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Dependency is the base class for cache dependency classes. |
12
|
|
|
* |
13
|
|
|
* Child classes should override its [[generateDependencyData()]] for generating |
14
|
|
|
* the actual dependency data. |
15
|
|
|
* |
16
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
17
|
|
|
* |
18
|
|
|
* @author Qiang Xue <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class Dependency extends \yii\base\BaseObject |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var mixed the dependency data that is saved in cache and later is compared with the |
25
|
|
|
* latest dependency data. |
26
|
|
|
*/ |
27
|
|
|
public $data; |
28
|
|
|
/** |
29
|
|
|
* @var bool whether this dependency is reusable or not. True value means that dependent |
30
|
|
|
* data for this cache dependency will be generated only once per request. This allows you |
31
|
|
|
* to use the same cache dependency for multiple separate cache calls while generating the same |
32
|
|
|
* page without an overhead of re-evaluating dependency data each time. Defaults to false. |
33
|
|
|
*/ |
34
|
|
|
public $reusable = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array static storage of cached data for reusable dependencies. |
38
|
|
|
*/ |
39
|
|
|
private static $_reusableData = []; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Evaluates the dependency by generating and saving the data related with dependency. |
44
|
|
|
* This method is invoked by cache before writing data into it. |
45
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
46
|
|
|
*/ |
47
|
36 |
|
public function evaluateDependency($cache) |
48
|
|
|
{ |
49
|
36 |
|
if ($this->reusable) { |
50
|
|
|
$hash = $this->generateReusableHash(); |
51
|
|
|
if (!array_key_exists($hash, self::$_reusableData)) { |
52
|
|
|
self::$_reusableData[$hash] = $this->generateDependencyData($cache); |
53
|
|
|
} |
54
|
|
|
$this->data = self::$_reusableData[$hash]; |
55
|
|
|
} else { |
56
|
36 |
|
$this->data = $this->generateDependencyData($cache); |
57
|
|
|
} |
58
|
36 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a value indicating whether the dependency has changed. |
62
|
|
|
* @deprecated since version 2.0.11. Will be removed in version 2.1. Use [[isChanged()]] instead. |
63
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
64
|
|
|
* @return bool whether the dependency has changed. |
65
|
|
|
*/ |
66
|
|
|
public function getHasChanged($cache) |
67
|
|
|
{ |
68
|
|
|
return $this->isChanged($cache); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks whether the dependency is changed. |
73
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
74
|
|
|
* @return bool whether the dependency has changed. |
75
|
|
|
* @since 2.0.11 |
76
|
|
|
*/ |
77
|
6 |
|
public function isChanged($cache) |
78
|
|
|
{ |
79
|
6 |
|
if ($this->reusable) { |
80
|
|
|
$hash = $this->generateReusableHash(); |
81
|
|
|
if (!array_key_exists($hash, self::$_reusableData)) { |
82
|
|
|
self::$_reusableData[$hash] = $this->generateDependencyData($cache); |
83
|
|
|
} |
84
|
|
|
$data = self::$_reusableData[$hash]; |
85
|
|
|
} else { |
86
|
6 |
|
$data = $this->generateDependencyData($cache); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
return $data !== $this->data; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Resets all cached data for reusable dependencies. |
94
|
|
|
*/ |
95
|
1 |
|
public static function resetReusableData() |
96
|
|
|
{ |
97
|
1 |
|
self::$_reusableData = []; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Generates a unique hash that can be used for retrieving reusable dependency data. |
102
|
|
|
* @return string a unique hash value for this cache dependency. |
103
|
|
|
* @see reusable |
104
|
|
|
*/ |
105
|
1 |
|
protected function generateReusableHash() |
106
|
|
|
{ |
107
|
1 |
|
$data = $this->data; |
108
|
1 |
|
$this->data = null; // https://github.com/yiisoft/yii2/issues/3052 |
109
|
1 |
|
$key = sha1(serialize($this)); |
110
|
1 |
|
$this->data = $data; |
111
|
1 |
|
return $key; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Generates the data needed to determine if dependency is changed. |
116
|
|
|
* Derived classes should override this method to generate the actual dependency data. |
117
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
118
|
|
|
* @return mixed the data needed to determine if dependency has been changed. |
119
|
|
|
*/ |
120
|
|
|
abstract protected function generateDependencyData($cache); |
121
|
|
|
} |
122
|
|
|
|