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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* TagDependency associates a cached data item with one or multiple [[tags]].
*
* By calling [[invalidate()]], you can invalidate all cached data items that are associated with the specified tag name(s).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TagDependency extends Dependency
{
/**
* @var string|array a list of tag names for this dependency. For a single tag, you may specify it as a string.
*/
public $tags = [];
/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
$newKeys = [];
foreach ($timestamps as $key => $timestamp) {
if ($timestamp === false) {
$newKeys[] = $key;
}
}
if (!empty($newKeys)) {
$timestamps = array_merge($timestamps, $this->touchKeys($cache, $newKeys));
}
return $timestamps;
}
/**
* Performs the actual dependency checking.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency is changed or not.
*/
public function getHasChanged($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
return $timestamps !== $this->data;
}
/**
* Invalidates all of the cached data items that are associated with any of the specified [[tags]].
* @param Cache $cache the cache component that caches the data items
* @param string|array $tags
*/
public static function invalidate($cache, $tags)
{
$keys = [];
foreach ((array) $tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
static::touchKeys($cache, $keys);
}
/**
* Generates the timestamp for the specified cache keys.
* @param Cache $cache
* @param string[] $keys
* @return array the timestamp indexed by cache keys
*/
protected static function touchKeys($cache, $keys)
{
$items = [];
$time = microtime();
foreach ($keys as $key) {
$items[$key] = $time;
}
$cache->mset($items);
return $items;
}
/**
* Returns the timestamps for the specified tags.
* @param Cache $cache
* @param string[] $tags
* @return array the timestamps indexed by the specified tags.
*/
protected function getTimestamps($cache, $tags)
{
if (empty($tags)) {
return [];
}
$keys = [];
foreach ($tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
return $cache->mget($keys);
}
}