|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache\Dependency; |
|
4
|
|
|
|
|
5
|
|
|
use Yiisoft\Cache\CacheInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Dependency is the base class for cache dependency classes. |
|
9
|
|
|
* |
|
10
|
|
|
* Child classes should override its {@see Dependency::generateDependencyData()} for generating |
|
11
|
|
|
* the actual dependency data. |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Dependency |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var mixed the dependency data that is saved in cache and later is compared with the |
|
17
|
|
|
* latest dependency data |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $data; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool whether this dependency is reusable or not. True value means that dependent |
|
23
|
|
|
* data for this cache dependency will be generated only once per request. This allows you |
|
24
|
|
|
* to use the same cache dependency for multiple separate cache calls while generating the same |
|
25
|
|
|
* page without an overhead of re-evaluating dependency data each time. Defaults to false. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $isReusable = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array static storage of cached data for reusable dependencies. |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $reusableData = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Changes dependency behavior so dependent data for this cache dependency will be generated only once per request. |
|
36
|
|
|
* This allows you to use the same cache dependency for multiple separate cache calls while generating the same |
|
37
|
|
|
* page without an overhead of re-evaluating dependency data each time. |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function markAsReusable(): void |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->isReusable = true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Evaluates the dependency by generating and saving the data related with dependency. |
|
46
|
|
|
* This method is invoked by cache before writing data into it. |
|
47
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function evaluateDependency(CacheInterface $cache): void |
|
50
|
|
|
{ |
|
51
|
9 |
|
if ($this->isReusable) { |
|
52
|
2 |
|
$hash = $this->generateReusableHash(); |
|
53
|
2 |
|
if (!\array_key_exists($hash, self::$reusableData)) { |
|
54
|
2 |
|
self::$reusableData[$hash] = $this->generateDependencyData($cache); |
|
55
|
|
|
} |
|
56
|
2 |
|
$this->data = self::$reusableData[$hash]; |
|
57
|
|
|
} else { |
|
58
|
7 |
|
$this->data = $this->generateDependencyData($cache); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Checks whether the dependency is changed. |
|
64
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
|
65
|
|
|
* @return bool whether the dependency has changed. |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function isChanged(CacheInterface $cache): bool |
|
68
|
|
|
{ |
|
69
|
7 |
|
if ($this->isReusable) { |
|
70
|
1 |
|
$hash = $this->generateReusableHash(); |
|
71
|
1 |
|
if (!\array_key_exists($hash, self::$reusableData)) { |
|
72
|
|
|
self::$reusableData[$hash] = $this->generateDependencyData($cache); |
|
73
|
|
|
} |
|
74
|
1 |
|
$data = self::$reusableData[$hash]; |
|
75
|
|
|
} else { |
|
76
|
6 |
|
$data = $this->generateDependencyData($cache); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
7 |
|
return $data !== $this->data; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Resets all cached data for reusable dependencies. |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public static function resetReusableData(): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
self::$reusableData = []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks whether the dependency has already been evaluated. |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
7 |
|
public function isEvaluated(): bool |
|
95
|
|
|
{ |
|
96
|
7 |
|
return $this->data !== null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Generates a unique hash that can be used for retrieving reusable dependency data. |
|
101
|
|
|
* @return string a unique hash value for this cache dependency. |
|
102
|
|
|
* @see isReusable |
|
103
|
|
|
*/ |
|
104
|
3 |
|
protected function generateReusableHash(): string |
|
105
|
|
|
{ |
|
106
|
3 |
|
$data = $this->data; |
|
107
|
3 |
|
$this->data = null; // https://github.com/yiisoft/yii2/issues/3052 |
|
108
|
3 |
|
$key = sha1(serialize($this)); |
|
109
|
3 |
|
$this->data = $data; |
|
110
|
3 |
|
return $key; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Converts iterable to array |
|
115
|
|
|
* @param iterable $iterable |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
5 |
|
protected function iterableToArray(iterable $iterable): array |
|
119
|
|
|
{ |
|
120
|
5 |
|
return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Generates the data needed to determine if dependency is changed. |
|
125
|
|
|
* Derived classes should override this method to generate the actual dependency data. |
|
126
|
|
|
* @param CacheInterface $cache the cache component that is currently evaluating this dependency |
|
127
|
|
|
* @return mixed the data needed to determine if dependency has been changed. |
|
128
|
|
|
*/ |
|
129
|
|
|
abstract protected function generateDependencyData(CacheInterface $cache); |
|
130
|
|
|
} |
|
131
|
|
|
|