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
|
|
|
* TagDependency associates a cached data item with one or multiple [[tags]]. |
12
|
|
|
* |
13
|
|
|
* By calling [[invalidate()]], you can invalidate all cached data items that are associated with the specified tag name(s). |
14
|
|
|
* |
15
|
|
|
* ```php |
16
|
|
|
* // setting multiple cache keys to store data forever and tagging them with "user-123" |
17
|
|
|
* Yii::$app->cache->set('user_42_profile', '', 0, new TagDependency(['tags' => 'user-123'])); |
18
|
|
|
* Yii::$app->cache->set('user_42_stats', '', 0, new TagDependency(['tags' => 'user-123'])); |
19
|
|
|
* |
20
|
|
|
* // invalidating all keys tagged with "user-123" |
21
|
|
|
* TagDependency::invalidate(Yii::$app->cache, 'user-123'); |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
25
|
|
|
* |
26
|
|
|
* @author Qiang Xue <[email protected]> |
27
|
|
|
* @since 2.0 |
28
|
|
|
*/ |
29
|
|
|
class TagDependency extends Dependency |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string|array a list of tag names for this dependency. For a single tag, you may specify it as a string. |
33
|
|
|
*/ |
34
|
|
|
public $tags = []; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generates the data needed to determine if dependency has been changed. |
39
|
|
|
* This method does nothing in this class. |
40
|
|
|
* @param Cache $cache the cache component that is currently evaluating this dependency |
41
|
8 |
|
* @return mixed the data needed to determine if dependency has been changed. |
42
|
|
|
*/ |
43
|
8 |
|
protected function generateDependencyData($cache) |
44
|
|
|
{ |
45
|
8 |
|
$timestamps = $this->getTimestamps($cache, (array) $this->tags); |
46
|
8 |
|
|
47
|
8 |
|
$newKeys = []; |
48
|
5 |
|
foreach ($timestamps as $key => $timestamp) { |
49
|
5 |
|
if ($timestamp === false) { |
50
|
8 |
|
$newKeys[] = $key; |
51
|
8 |
|
} |
52
|
5 |
|
} |
53
|
5 |
|
if (!empty($newKeys)) { |
54
|
|
|
$timestamps = array_merge($timestamps, static::touchKeys($cache, $newKeys)); |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
return $timestamps; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
2 |
|
public function isChanged($cache) |
64
|
|
|
{ |
65
|
2 |
|
$timestamps = $this->getTimestamps($cache, (array) $this->tags); |
66
|
2 |
|
return $timestamps !== $this->data; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Invalidates all of the cached data items that are associated with any of the specified [[tags]]. |
71
|
|
|
* @param Cache $cache the cache component that caches the data items |
72
|
|
|
* @param string|array $tags |
73
|
|
|
*/ |
74
|
2 |
|
public static function invalidate($cache, $tags) |
75
|
|
|
{ |
76
|
2 |
|
$keys = []; |
77
|
2 |
|
foreach ((array) $tags as $tag) { |
78
|
2 |
|
$keys[] = $cache->buildKey([__CLASS__, $tag]); |
79
|
2 |
|
} |
80
|
2 |
|
static::touchKeys($cache, $keys); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Generates the timestamp for the specified cache keys. |
85
|
|
|
* @param Cache $cache |
86
|
|
|
* @param string[] $keys |
87
|
|
|
* @return array the timestamp indexed by cache keys |
88
|
|
|
*/ |
89
|
5 |
|
protected static function touchKeys($cache, $keys) |
90
|
|
|
{ |
91
|
5 |
|
$items = []; |
92
|
5 |
|
$time = microtime(); |
93
|
5 |
|
foreach ($keys as $key) { |
94
|
5 |
|
$items[$key] = $time; |
95
|
5 |
|
} |
96
|
5 |
|
$cache->multiSet($items); |
97
|
5 |
|
return $items; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the timestamps for the specified tags. |
102
|
|
|
* @param Cache $cache |
103
|
|
|
* @param string[] $tags |
104
|
|
|
* @return array the timestamps indexed by the specified tags. |
105
|
|
|
*/ |
106
|
8 |
|
protected function getTimestamps($cache, $tags) |
107
|
|
|
{ |
108
|
8 |
|
if (empty($tags)) { |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
$keys = []; |
113
|
8 |
|
foreach ($tags as $tag) { |
114
|
8 |
|
$keys[] = $cache->buildKey([__CLASS__, $tag]); |
115
|
8 |
|
} |
116
|
|
|
|
117
|
8 |
|
return $cache->multiGet($keys); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|