Passed
Push — main ( 384d6d...29b1f6 )
by Sílvio
01:03 queued 15s
created

Cacheer::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 4
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp;
4
5
use Closure;
6
use Silviooosilva\CacheerPhp\Interface\CacheerInterface;
7
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore;
8
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore;
9
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore;
10
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore;
11
use Silviooosilva\CacheerPhp\Helpers\CacheConfig;
12
use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter;
13
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
14
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper;
15
use RuntimeException;
16
17
/**
18
 * Class CacheerPHP
19
 * @author Sílvio Silva <https://github.com/silviooosilva>
20
 * @package Silviooosilva\CacheerPhp
21
 */
22
final class Cacheer implements CacheerInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private string $message;
28
29
    /**
30
     * @var boolean
31
     */
32
    private bool $success;
33
34
    /**
35
     * @var boolean
36
     */
37
    private bool $formatted = false;
38
39
    /**
40
     * @var bool
41
     */
42
    private bool $compression = false;
43
44
    /**
45
     * @var string|null
46
     */
47
    private ?string $encryptionKey = null;
48
49
    /**
50
     * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore
51
     */
52
    public $cacheStore;
53
54
    /**
55
     * @var array
56
     */
57
    public array $options = [];
58
59
    public function __construct(array $options = [], $formatted = false)
60
    {
61
        $this->formatted = $formatted;
62
        $this->validateOptions($options);
63
        $this->setDriver()->useDefaultDriver();
64
    }
65
66
    /**
67
     * @param string $cacheKey
68
     * @param mixed  $cacheData
69
     * @param string $namespace
70
     * @param int|string $ttl
71
     * @return bool
72
     */
73
    public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
74
    {
75
        if (!empty($this->getCache($cacheKey, $namespace))) {
76
            return true;
77
        }
78
79
        $this->putCache($cacheKey, $cacheData, $namespace, $ttl);
80
        $this->setMessage($this->getMessage(), $this->isSuccess());
81
82
        return false;
83
    }
84
85
    /**
86
     * @param string $cacheKey
87
     * @param mixed  $cacheData
88
     * @param string $namespace
89
     * @return void
90
     */
91
    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
92
    {
93
        $this->cacheStore->appendCache($cacheKey, $cacheData, $namespace);
94
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
95
    }
96
97
    /**
98
     * @param string $cacheKey
99
     * @param string $namespace
100
     * @return void
101
     */
102
    public function clearCache(string $cacheKey, string $namespace = '')
103
    {
104
        $this->cacheStore->clearCache($cacheKey, $namespace);
105
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
106
    }
107
108
    /**
109
     * @param string $cacheKey
110
     * @param int $amount
111
     * @param string $namespace
112
     * @return bool
113
     */
114
    public function decrement(string $cacheKey, int $amount = 1, string $namespace = '')
115
    {
116
        return $this->increment($cacheKey, ($amount * -1), $namespace);
117
    }
118
119
    /**
120
     * @param string $cacheKey
121
     * @param mixed $cacheData
122
     * @return void
123
     */
124
    public function forever(string $cacheKey, mixed $cacheData)
125
    {
126
        $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000);
127
        $this->setMessage($this->getMessage(), $this->isSuccess());
128
    }
129
130
    /**
131
     * @return void
132
     */
133
    public function flushCache()
134
    {
135
        $this->cacheStore->flushCache();
136
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
137
    }
138
139
    /**
140
     * @param string $cacheKey
141
     * @param string $namespace
142
     * @return mixed
143
     */
144
    public function getAndForget(string $cacheKey, string $namespace = '')
145
    {
146
        $cachedData = $this->getCache($cacheKey, $namespace);
147
148
        if (!empty($cachedData)) {
149
            $this->setMessage("Cache retrieved and deleted successfully!", true);
150
            $this->clearCache($cacheKey, $namespace);
151
            return $cachedData;
152
        }
153
154
        return null;
155
    }
156
157
    /**
158
     * @param string $cacheKey
159
     * @param string $namespace
160
     * @param string|int $ttl
161
     * @return CacheDataFormatter|mixed
162
     */
163
    public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
164
    {
165
        $cacheData = $this->cacheStore->getCache($cacheKey, $namespace, $ttl);
166
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
167
168
        if ($this->cacheStore->isSuccess() && ($this->compression || $this->encryptionKey !== null)) {
169
            $cacheData = CacheerHelper::recoverFromStorage($cacheData, $this->compression, $this->encryptionKey);
170
        }
171
172
        return $this->formatted ? new CacheDataFormatter($cacheData) : $cacheData;
173
    }
174
175
    /**
176
     * @param string $cacheKey
177
     * @param string $namespace
178
     * @return void
179
     */
180
    public function has(string $cacheKey, string $namespace = '')
181
    {
182
        $this->cacheStore->has($cacheKey, $namespace);
183
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
184
    }
185
186
    /**
187
     * @param string $cacheKey
188
     * @param int $amount
189
     * @param string $namespace
190
     * @return bool
191
     */
192
    public function increment(string $cacheKey, int $amount = 1, string $namespace = '')
193
    {
194
        $cacheData = $this->getCache($cacheKey, $namespace);
195
196
        if(!empty($cacheData) && is_numeric($cacheData)) {
197
            $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace);
198
            $this->setMessage($this->getMessage(), $this->isSuccess());
199
            return true;
200
        }
201
202
        return false;
203
    }
204
205
    /**
206
     * @return boolean
207
     */
208
    public function isSuccess()
209
    {
210
        return $this->success;
211
    }
212
213
    /**
214
     * @param string $cacheKey
215
     * @param mixed  $cacheData
216
     * @param string $namespace
217
     * @param string|int $ttl
218
     * @return void
219
     */
220
    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600)
221
    {
222
        $data = CacheerHelper::prepareForStorage($cacheData, $this->compression, $this->encryptionKey);
223
        $this->cacheStore->putCache($cacheKey, $data, $namespace, $ttl);
224
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
225
    }
226
227
    /**
228
     * @param array   $items
229
     * @param string  $namespace
230
     * @param integer $batchSize
231
     * @return void
232
     */
233
    public function putMany(array $items, string $namespace = '', int $batchSize = 100)
234
    {
235
        $this->cacheStore->putMany($items, $namespace, $batchSize);
236
    }
237
238
    /**
239
     * @param string $cacheKey
240
     * @param string|int $ttl
241
     * @param string $namespace
242
     * @return void
243
     */
244
    public function renewCache(string $cacheKey, string|int $ttl = 3600, string $namespace = '')
245
    {
246
        $this->cacheStore->renewCache($cacheKey, $ttl, $namespace);
247
248
        if ($this->cacheStore->isSuccess()) {
249
            $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
250
        } else {
251
            $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
252
        }
253
    }
254
255
    /**
256
     * @param string $cacheKey
257
     * @param int|string $ttl
258
     * @param Closure $callback
259
     * @return mixed
260
     */
261
    public function remember(string $cacheKey, int|string $ttl, Closure $callback)
262
    {
263
        $cachedData = $this->getCache($cacheKey, ttl: $ttl);
264
265
        if(!empty($cachedData)) {
266
            return $cachedData;
267
        }
268
269
        $cacheData = $callback();
270
        $this->putCache($cacheKey, $cacheData, ttl: $ttl);
271
        $this->setMessage($this->getMessage(), $this->isSuccess());
272
273
        return $cacheData;
274
    }
275
276
    /**
277
     * @param string $cacheKey
278
     * @param Closure $callback
279
     * @return mixed
280
     */
281
    public function rememberForever(string $cacheKey, Closure $callback)
282
    {
283
        return $this->remember($cacheKey, 31536000 * 1000, $callback);
284
    }
285
286
    /**
287
     * @return CacheConfig
288
     */
289
    public function setConfig()
290
    {
291
        return new CacheConfig($this);
292
    }
293
294
    /**
295
     * @return CacheDriver
296
     */
297
    public function setDriver()
298
    {
299
        return new CacheDriver($this);
300
    }
301
302
    /**
303
     * @param string  $message
304
     * @param boolean $success
305
     * @return void
306
     */
307
    private function setMessage(string $message, bool $success)
308
    {
309
        $this->message = $message;
310
        $this->success = $success;
311
    }
312
313
    /**
314
     * @return string
315
     */
316
    public function getMessage()
317
    {
318
        return $this->message;
319
    }
320
321
    /**
322
     * @return void
323
     */
324
    public function useFormatter()
325
    {
326
        $this->formatted = !$this->formatted;
327
    }
328
329
    /**
330
     * @param array $options
331
     * @return void
332
     */
333
    private function validateOptions(array $options)
334
    {
335
        $this->options = $options;
336
    }
337
338
    /**
339
     * Enable or disable data compression
340
     *
341
     * @param bool $status
342
     * @return $this
343
     */
344
    public function useCompression(bool $status = true)
345
    {
346
        $this->compression = $status;
347
        return $this;
348
    }
349
350
    /**
351
     * Enable encryption for cached data
352
     *
353
     * @param string $key
354
     * @return $this
355
     */
356
    public function useEncryption(string $key)
357
    {
358
        $this->encryptionKey = $key;
359
        return $this;
360
    }
361
}
362