-
Notifications
You must be signed in to change notification settings - Fork 5
/
Dependency.php
138 lines (125 loc) · 2.93 KB
/
Dependency.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace TaggedCache;
/**
* Class Dependency
* @package TaggedCache
*/
class Dependency implements \ICacheDependency
{
/**
* List of tags
* Array of string or Tag[]
* @var string[]|Tag[]|null
*/
public $tags = null;
/**
* List of tags versions
* @var null
*/
public $versions = null;
function __construct(array $tags)
{
$this->tags = array();
foreach ($tags as $tag)
{
$this->tags[] = ($tag instanceof Tag) ? $tag->getTag() : $tag;
}
}
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
*/
public function evaluateDependency()
{
if ($this->tags === null || !is_array($this->tags))
{
return;
}
$tagsWithoutVersion = $tagsWithVersion = $tags = array();
foreach ($this->tags as $tag)
{
if (!($tag instanceof Tag))
{
$tag = new Tag($tag);
}
$tagKey = $tag->getTag();
$tags[$tagKey] = $tag;
if (false === ($version = $tag->get()))
{
$tagsWithoutVersion[] = $tagKey;
} else
{
$tagsWithVersion[$tagKey] = $version;
}
}
// load unknown tags from cache
$versions = !empty($tagsWithoutVersion) ? \Yii::app()->cache->mget($tagsWithoutVersion) : array();
foreach ($versions as $tagKey => $version)
{
// merge known and loaded tags
$tagsWithVersion[$tagKey] = $version;
}
$this->versions = $tagsWithVersion;
foreach ($tags as $tag => $tagInst)
{
if (!isset($this->versions[$tag]) || false === $this->versions[$tag])
{
// generate tags that are not in cache
$this->versions[$tag] = $version = $this->getNewTagVersion();
$tagInst->set($version)->save();
}
}
ksort($this->versions);
}
/**
* @return boolean whether the dependency has changed.
*/
public function getHasChanged()
{
if ($this->versions === null || !is_array($this->versions))
{
return true;
}
$tagsWithoutVersion = $tagsWithVersion = $tags = array();
foreach ($this->tags as $tag)
{
if (!($tag instanceof Tag))
{
$tag = new Tag($tag);
}
$tags[$tag->getTag()] = $tag;
if (false === ($version = $tag->get()))
{
$tagsWithoutVersion[] = $tag->getTag();
} else
{
$tagsWithVersion[$tag->getTag()] = $version;
}
}
$versions = !empty($tagsWithoutVersion) ? \Yii::app()->cache->mget($tagsWithoutVersion) : array();
foreach ($versions as $tag => $version)
{
$tagsWithVersion[$tag] = $version;
$tags[$tag]->set($version);
}
foreach ($this->versions as $tag => $savedTagVersion)
{
// if is absent in cache or saved versions differs from current version
if (!isset($tagsWithVersion[$tag]) || $savedTagVersion != $tagsWithVersion[$tag])
{
return true;
}
}
return false;
}
/**
* Returns new unique version string
* @return string
*/
protected function getNewTagVersion()
{
static $counter = 0;
$counter++;
return md5(microtime() . getmypid() . uniqid('')) . '_' . $counter;
}
}