Repository navigation
Expand file tree
/
Copy pathCacheStorage.php
More file actions
executable file
·241 lines (217 loc) · 6.67 KB
/
Copy pathCacheStorage.php
File metadata and controls
executable file
·241 lines (217 loc) · 6.67 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Cache;
use Webiny\Component\Cache\Bridge\CacheStorageInterface;
/**
* CacheStorage is the main instance for working with cache drivers.
* The best way to create a CacheStorage instance is over the Cache class.
*
* @package Webiny\Component\Cache
*/
class CacheStorage
{
/**
* @var null|Null
*/
private static $_nullDriver = null;
/**
* @var \Webiny\Component\Cache\Bridge\CacheStorageInterface
*/
private $_driver;
/**
* @var array
*/
private $_options = [
'status' => true,
'ttl' => 86400
];
/**
* Create a cache driver instance.
*
* @param CacheStorageInterface $driver Instance of CacheInterface.
* @param array $options Array of options.
*/
function __construct(CacheStorageInterface $driver, array $options = [])
{
$this->_driver = $driver;
foreach ($options as $k => $v) {
if (isset($this->_options[$k])) {
$this->_options[$k] = $v;
}
}
}
/**
* Get driver instance.
*
* @return CacheStorageInterface
*/
function getDriver()
{
// if driver status is false, we return the Null driver
if (!$this->getStatus()) {
if (is_null(self::$_nullDriver)) {
self::$_nullDriver = new Storage\Null();
}
return self::$_nullDriver;
}
return $this->_driver;
}
/**
* Get cache status.
*
* @return mixed
*/
function getStatus()
{
return $this->_options['status'];
}
/**
* Change the cache status.
*
* @param bool $status Turn caching on or off.
*/
function setStatus($status)
{
$this->_options['status'] = (bool)$status;
}
/**
* Returns the ttl from options.
*
* @return int
*/
function getTtl()
{
return $this->_options['ttl'];
}
/**
* Save a value into memory only if it DOESN'T exists (or false will be returned).
*
* @param string $key Name of the key.
* @param mixed $value Value you wish to save.
* @param int $ttl For how long to store value. (in seconds)
* @param array|string $tags Tags you wish to assign to this cache entry.
*
* @return boolean True if value was added, otherwise false.
*/
public function add($key, $value, $ttl = null, $tags = null)
{
return $this->getDriver()->add($key, $value, (is_null($ttl) ? $this->getTtl() : $ttl), $tags);
}
/**
* Save a value into memory.
*
* @param string $key Name of the key.
* @param mixed $value Value you wish to save.
* @param int $ttl For how long to store value. (in seconds)
* @param array|string $tags Tags you wish to assign to this cache entry.
*
* @return bool True if value was stored successfully, otherwise false.
*/
public function save($key, $value, $ttl = null, $tags = null)
{
return $this->getDriver()->save($key, $value, (is_null($ttl) ? $this->getTtl() : $ttl), $tags);
}
/**
* Get the cache data for the given $key.
*
* @param string|array $key Name of the cache key.
* @param mixed $ttlLeft = (ttl - time()) of key. Use to exclude dog-pile effect, with lock/unlock_key methods.
*
* @return mixed
*/
public function read($key, &$ttlLeft = -1)
{
return $this->getDriver()->read($key, $ttlLeft);
}
/**
* Delete key or array of keys from storage.
*
* @param string|array $key Key, or array of keys, you wish to delete.
*
* @return boolean|array If array of keys was passed, on error will be returned array of not deleted keys, or true on success.
*/
public function delete($key)
{
return $this->getDriver()->delete($key);
}
/**
* Delete expired cache values.
*
* @return boolean
*/
public function deleteOld()
{
return $this->getDriver()->deleteOld();
}
/**
* Delete keys by tags.
*
* @param array|string $tag Tag, or an array of tags, for which you wish to delete the cache.
*
* @return boolean
*/
public function deleteByTags($tag)
{
return $this->getDriver()->deleteByTags($tag);
}
/**
* Select from storage via callback function.
* Only values of array type will be selected.
*
* @param callable $callback ($value_array,$key)
* @param bool $getArray
*
* @return mixed
*/
public function selectByCallback($callback, $getArray = false)
{
return $this->getDriver()->selectByCallback($callback, $getArray);
}
/**
* Increment value of the key.
*
* @param string $key Name of the cache key.
* @param mixed $byValue
* If stored value is an array:
* - If $by_value is a value in array, new element will be pushed to the end of array,
* - If $by_value is a key=>value array, new key=>value pair will be added (or updated).
* @param int $limitKeysCount Maximum count of elements (used only if stored value is array).
* @param int $ttl Set time to live for key.
*
* @return int|string|array New key value.
*/
public function increment($key, $byValue = 1, $limitKeysCount = 0, $ttl = 259200)
{
return $this->getDriver()->increment($key, $byValue, $limitKeysCount, $ttl);
}
/**
* Get exclusive mutex for key. Key will be still accessible to read and write, but
* another process can exclude dog-pile effect, if before updating the key he will try to get this mutex.
*
* @param mixed $key Name of the cache key.
* @param mixed $autoUnlockerVariable Pass empty, just declared variable
*
* @return bool
*/
public function lockKey($key, &$autoUnlockerVariable)
{
return $this->getDriver()->lockKey($key, $autoUnlockerVariable);
}
/**
* Try to lock key, and if key is already locked - wait, until key will be unlocked.
* Time of waiting is defined in max_wait_unlock constant of MemoryObject class.
*
* @param string $key Name of the cache key.
* @param $autoUnlocker
*
* @return boolean
*/
public function acquireKey($key, &$autoUnlocker)
{
return $this->getDriver()->acquireKey($key, $autoUnlocker);
}
}