1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\CacheStore; |
4
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\Utils\CacheLogger; |
6
|
|
|
use Silviooosilva\CacheerPhp\Interface\CacheerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ArrayCacheStore |
10
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
11
|
|
|
* @package Silviooosilva\CacheerPhp |
12
|
|
|
*/ |
13
|
|
|
class ArrayCacheStore implements CacheerInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param array $arrayStore |
18
|
|
|
*/ |
19
|
|
|
private array $arrayStore = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param boolean |
23
|
|
|
*/ |
24
|
|
|
private bool $success = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string |
28
|
|
|
*/ |
29
|
|
|
private string $message = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CacheLogger |
33
|
|
|
*/ |
34
|
|
|
private $logger = null; |
35
|
|
|
|
36
|
|
|
public function __construct(string $logPath) |
37
|
|
|
{ |
38
|
|
|
$this->logger = new CacheLogger($logPath); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $cacheKey |
43
|
|
|
* @param string $namespace |
44
|
|
|
* @param int|string $ttl |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
51
|
|
|
|
52
|
|
|
if (!$this->has($cacheKey, $namespace)) { |
53
|
|
|
$this->setMessage("cacheData not found, does not exists or expired", false); |
54
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$cacheData = $this->arrayStore[$arrayStoreKey]; |
59
|
|
|
$expirationTime = $cacheData['expirationTime'] ?? 0; |
60
|
|
|
$now = time(); |
61
|
|
|
|
62
|
|
|
if($expirationTime !== 0 && $now >= $expirationTime) { |
63
|
|
|
list($np, $key) = explode(':', $arrayStoreKey); |
64
|
|
|
$this->clearCache($key, $np); |
65
|
|
|
$this->setMessage("cacheKey: {$key} has expired.", false); |
66
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->setMessage("Cache retrieved successfully", true); |
71
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
72
|
|
|
return $this->serialize($cacheData['value'], false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $cacheKey |
77
|
|
|
* @param mixed $cacheData |
78
|
|
|
* @param string $namespace |
79
|
|
|
* @param int|string $ttl |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
86
|
|
|
|
87
|
|
|
$this->arrayStore[$arrayStoreKey] = [ |
88
|
|
|
'value' => serialize($cacheData), |
89
|
|
|
'expirationTime' => time() + $ttl |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$this->setMessage("Cache stored successfully", true); |
93
|
|
|
$this->logger->debug("{$this->getMessage()} from Array driver."); |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $items |
99
|
|
|
* @param string $namespace |
100
|
|
|
* @param int $batchSize |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$chunks = array_chunk($items, $batchSize, true); |
107
|
|
|
|
108
|
|
|
foreach ($chunks as $chunk) { |
109
|
|
|
foreach ($chunk as $key => $data) { |
110
|
|
|
$this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$this->setMessage("{$this->getMessage()}", $this->isSuccess()); |
114
|
|
|
$this->logger->debug("{$this->getMessage()} from Array driver."); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $cacheKey |
119
|
|
|
* @param mixed $cacheData |
120
|
|
|
* @param string $namespace |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
124
|
|
|
{ |
125
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
126
|
|
|
|
127
|
|
|
if (!$this->has($cacheKey, $namespace)) { |
128
|
|
|
$this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
129
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$currentValue = $this->arrayStore[$arrayStoreKey]['value']; |
134
|
|
|
if (is_string($currentValue)) { |
135
|
|
|
$currentValue = unserialize($currentValue); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->arrayStore[$arrayStoreKey]['value'] = serialize($cacheData); |
139
|
|
|
$this->setMessage("Cache appended successfully", true); |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $cacheKey |
147
|
|
|
* @param string $namespace |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function has(string $cacheKey, string $namespace = '') |
151
|
|
|
{ |
152
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
153
|
|
|
return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $cacheKey |
158
|
|
|
* @param int|string $ttl |
159
|
|
|
* @param string $namespace |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
163
|
|
|
{ |
164
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
165
|
|
|
|
166
|
|
|
if (isset($this->arrayStore[$arrayStoreKey])) { |
167
|
|
|
$ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time(); |
168
|
|
|
$this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
169
|
|
|
$this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
170
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $cacheKey |
176
|
|
|
* @param string $namespace |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function clearCache(string $cacheKey, string $namespace = '') |
180
|
|
|
{ |
181
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
182
|
|
|
unset($this->arrayStore[$arrayStoreKey]); |
183
|
|
|
$this->setMessage("Cache cleared successfully", true); |
184
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function flushCache() |
192
|
|
|
{ |
193
|
|
|
unset($this->arrayStore); |
194
|
|
|
$this->arrayStore = []; |
195
|
|
|
$this->setMessage("Cache flushed successfully", true); |
196
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $message |
201
|
|
|
* @param boolean $success |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
private function setMessage(string $message, bool $success) |
205
|
|
|
{ |
206
|
|
|
$this->message = $message; |
207
|
|
|
$this->success = $success; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getMessage() |
215
|
|
|
{ |
216
|
|
|
return $this->message; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return boolean |
221
|
|
|
*/ |
222
|
|
|
public function isSuccess() |
223
|
|
|
{ |
224
|
|
|
return $this->success; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $cacheKey |
229
|
|
|
* @param string $namespace |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
private function buildArrayKey(string $cacheKey, string $namespace = '') |
233
|
|
|
{ |
234
|
|
|
return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param mixed $data |
239
|
|
|
* @param bool $serialize |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
private function serialize(mixed $data, bool $serialize = true) |
243
|
|
|
{ |
244
|
|
|
return $serialize ? serialize($data) : unserialize($data); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} |
248
|
|
|
|