-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemCache.php
172 lines (148 loc) · 4.62 KB
/
MemCache.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace yii1tech\cache\tagged;
use CMemCache;
use Memcached;
/**
* MemCache is tag aware version of standard {@see \CMemCache}.
*
* It saves tagged cache item keys into additional separated entities inside MemCache.
*
* > Note: unlike {@see \CMemCache} this class does not support "memcache" PHP extension and requires usage of "memcached" instead.
*
* > Warning: this solution may be unreliable depending on actual MemCache server configuration, since MemCache may delete random keys
* without prompt to optimize memory usage, the tags information may become lost, while keys related to it still remain.
*
* Application configuration example:
*
* ```php
* return [
* 'components' => [
* 'cache' => [
* 'class' => \yii1tech\cache\tagged\MemCache::class,
* 'servers' => [
* [
* 'host' => 'server1',
* 'port' => 11211,
* 'weight' => 60,
* ],
* [
* 'host' => 'server2',
* 'port' => 11211,
* 'weight' => 40,
* ],
* ],
* ],
* // ...
* ],
* // ...
* ];
* ```
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class MemCache extends CMemCache implements TagAwareCacheContract
{
/**
* {@inheritdoc}
*/
public $useMemcached = true;
/**
* @var string key prefix for the tag data entries.
*/
public $tagKeyPrefix = '__tag_';
/**
* {@inheritdoc}
*/
public function set($id, $value, $expire = 0, $dependency = null, array $tags = [])
{
if (!parent::set($id, $value, $expire, $dependency)) {
return false;
}
$result = true;
foreach ($tags as $tag) {
if (!$this->tagKey($tag, $id)) {
$result = false;
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function add($id, $value, $expire = 0, $dependency = null, array $tags = [])
{
if (!parent::add($id, $value, $expire, $dependency)) {
return false;
}
$result = true;
foreach ($tags as $tag) {
if (!$this->tagKey($tag, $id)) {
$result = false;
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function invalidateTags(array $tags): bool
{
$result = true;
foreach ($tags as $tag) {
$tagKey = $this->generateUniqueKey($this->tagKeyPrefix . $tag);
$value = $this->getValue($tagKey);
if (!empty($value)) {
$keys = json_decode($value);
if (!empty($keys)) {
$deleteResults = $this->getMemCache()->deleteMulti($keys);
if (in_array(false, $deleteResults, true)) {
$result = false;
}
}
}
if (!$this->deleteValue($tagKey)) {
$result = false;
}
}
return $result;
}
/**
* Adds given tag to specified cache key.
*
* @param string $tag tag name.
* @param string $key cache key to be tagged.
* @return bool whether operation is successful.
*/
protected function tagKey(string $tag, string $key): bool
{
$tagKey = $this->generateUniqueKey($this->tagKeyPrefix . $tag);
$key = $this->generateUniqueKey($key);
$memcache = $this->getMemCache();
while (true) {
$data = $memcache->get($tagKey, null, Memcached::GET_EXTENDED);
if ($data === false || $memcache->getResultCode() === Memcached::RES_NOTFOUND) {
$value = json_encode([$key]);
$memcache->add($tagKey, $value);
} else {
$value = $data['value'];
$casToken = $data['cas'];
$existingKeys = json_decode($value);
if (empty($existingKeys)) {
$value = json_encode([$key]);
} else {
$value = json_encode(array_merge($existingKeys, [$key]));
}
$memcache->cas($casToken, $tagKey, $value);
}
$resultCode = $memcache->getResultCode();
if ($resultCode === Memcached::RES_DATA_EXISTS) {
continue;
}
if (in_array($resultCode, [Memcached::RES_SUCCESS, Memcached::RES_END, Memcached::RES_STORED], true)) {
return true;
}
return false;
}
}
}