|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MySQLReplication\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use MySQLReplication\Config\Config; |
|
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ArrayCache |
|
10
|
|
|
* @package MySQLReplication\Cache |
|
11
|
|
|
*/ |
|
12
|
|
|
class ArrayCache implements CacheInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $tableMapCache = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Fetches a value from the cache. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $key The unique key of this item in the cache. |
|
23
|
|
|
* @param mixed $default Default value to return if the key does not exist. |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
28
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
29
|
|
|
*/ |
|
30
|
52 |
|
public function get($key, $default = null) |
|
31
|
|
|
{ |
|
32
|
52 |
|
return $this->has($key) ? self::$tableMapCache[$key] : $default; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $key The key of the item to store. |
|
39
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
|
40
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
41
|
|
|
* the driver supports TTL then the library may set a default value |
|
42
|
|
|
* for it or let the driver take care of that. |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool True on success and false on failure. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
47
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
48
|
|
|
*/ |
|
49
|
52 |
|
public function set($key, $value, $ttl = null) |
|
50
|
|
|
{ |
|
51
|
|
|
// automatically clear table cache to save memory |
|
52
|
52 |
|
if (count(self::$tableMapCache) > Config::getTableCacheSize()) { |
|
53
|
|
|
self::$tableMapCache = array_slice( |
|
54
|
|
|
self::$tableMapCache, |
|
55
|
|
|
ceil(Config::getTableCacheSize() / 2), |
|
56
|
|
|
null, |
|
57
|
|
|
true |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
52 |
|
self::$tableMapCache[$key] = $value; |
|
62
|
|
|
|
|
63
|
52 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Delete an item from the cache by its unique key. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $key The unique cache key of the item to delete. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool True if the item was successfully removed. False if there was an error. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
74
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function delete($key) |
|
77
|
|
|
{ |
|
78
|
|
|
unset(self::$tableMapCache[$key]); |
|
79
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Wipes clean the entire cache's keys. |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool True on success and false on failure. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function clear() |
|
89
|
|
|
{ |
|
90
|
|
|
self::$tableMapCache = []; |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Obtains multiple cache items by their unique keys. |
|
97
|
|
|
* |
|
98
|
|
|
* @param iterable $keys A list of keys that can obtained in a single operation. |
|
99
|
|
|
* @param mixed $default Default value to return for keys that do not exist. |
|
100
|
|
|
* |
|
101
|
|
|
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
104
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
|
105
|
|
|
* or if any of the $keys are not a legal value. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getMultiple($keys, $default = null) |
|
108
|
|
|
{ |
|
109
|
|
|
$data = []; |
|
110
|
|
|
foreach ($keys as $key) { |
|
111
|
|
|
if ($this->has($key)) { |
|
112
|
|
|
$data[$key] = self::$tableMapCache[$key]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return [] !== $data ? $data : $default; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
|
121
|
|
|
* |
|
122
|
|
|
* @param iterable $values A list of key => value pairs for a multiple-set operation. |
|
123
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
124
|
|
|
* the driver supports TTL then the library may set a default value |
|
125
|
|
|
* for it or let the driver take care of that. |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool True on success and false on failure. |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
130
|
|
|
* MUST be thrown if $values is neither an array nor a Traversable, |
|
131
|
|
|
* or if any of the $values are not a legal value. |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setMultiple($values, $ttl = null) |
|
134
|
|
|
{ |
|
135
|
|
|
foreach ($values as $key => $value) { |
|
136
|
|
|
$this->set($key, $value); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Deletes multiple cache items in a single operation. |
|
144
|
|
|
* |
|
145
|
|
|
* @param iterable $keys A list of string-based keys to be deleted. |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
|
148
|
|
|
* |
|
149
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
150
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
|
151
|
|
|
* or if any of the $keys are not a legal value. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function deleteMultiple($keys) |
|
154
|
|
|
{ |
|
155
|
|
|
foreach ($keys as $key) { |
|
156
|
|
|
$this->delete($key); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Determines whether an item is present in the cache. |
|
164
|
|
|
* |
|
165
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes |
|
166
|
|
|
* and not to be used within your live applications operations for get/set, as this method |
|
167
|
|
|
* is subject to a race condition where your has() will return true and immediately after, |
|
168
|
|
|
* another script can remove it making the state of your app out of date. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $key The cache item key. |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool |
|
173
|
|
|
* |
|
174
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
175
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
176
|
|
|
*/ |
|
177
|
52 |
|
public function has($key) |
|
178
|
|
|
{ |
|
179
|
52 |
|
return isset(self::$tableMapCache[$key]); |
|
180
|
|
|
} |
|
181
|
|
|
} |