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['cacheData'], 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
|
|
|
'cacheData' => 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
|
|
|
$this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
134
|
|
|
$this->setMessage("Cache appended successfully", true); |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $cacheKey |
142
|
|
|
* @param string $namespace |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function has(string $cacheKey, string $namespace = '') |
146
|
|
|
{ |
147
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
148
|
|
|
return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $cacheKey |
153
|
|
|
* @param int|string $ttl |
154
|
|
|
* @param string $namespace |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
158
|
|
|
{ |
159
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
160
|
|
|
|
161
|
|
|
if (isset($this->arrayStore[$arrayStoreKey])) { |
162
|
|
|
$ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time(); |
163
|
|
|
$this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
164
|
|
|
$this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
165
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $cacheKey |
171
|
|
|
* @param string $namespace |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function clearCache(string $cacheKey, string $namespace = '') |
175
|
|
|
{ |
176
|
|
|
$arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
177
|
|
|
unset($this->arrayStore[$arrayStoreKey]); |
178
|
|
|
$this->setMessage("Cache cleared successfully", true); |
179
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function flushCache() |
187
|
|
|
{ |
188
|
|
|
unset($this->arrayStore); |
189
|
|
|
$this->arrayStore = []; |
190
|
|
|
$this->setMessage("Cache flushed successfully", true); |
191
|
|
|
$this->logger->debug("{$this->getMessage()} from array driver."); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $message |
196
|
|
|
* @param boolean $success |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
private function setMessage(string $message, bool $success) |
200
|
|
|
{ |
201
|
|
|
$this->message = $message; |
202
|
|
|
$this->success = $success; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getMessage() |
210
|
|
|
{ |
211
|
|
|
return $this->message; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return boolean |
216
|
|
|
*/ |
217
|
|
|
public function isSuccess() |
218
|
|
|
{ |
219
|
|
|
return $this->success; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $cacheKey |
224
|
|
|
* @param string $namespace |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
private function buildArrayKey(string $cacheKey, string $namespace = '') |
228
|
|
|
{ |
229
|
|
|
return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param mixed $data |
234
|
|
|
* @param bool $serialize |
235
|
|
|
* @return mixed |
236
|
|
|
*/ |
237
|
|
|
private function serialize(mixed $data, bool $serialize = true) |
238
|
|
|
{ |
239
|
|
|
return $serialize ? serialize($data) : unserialize($data); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|