|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
|
6
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; |
|
7
|
|
|
|
|
8
|
|
|
class CacheMutator |
|
9
|
|
|
{ |
|
10
|
|
|
private Cacheer $cacheer; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(Cacheer $cacheer) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->cacheer = $cacheer; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!empty($this->cacheer->getCache($cacheKey, $namespace))) { |
|
20
|
|
|
return true; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$this->putCache($cacheKey, $cacheData, $namespace, $ttl); |
|
24
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
25
|
|
|
|
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = ''): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->cacheer->cacheStore->appendCache($cacheKey, $cacheData, $namespace); |
|
32
|
|
|
$this->cacheer->syncState(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function clearCache(string $cacheKey, string $namespace = ''): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cacheer->cacheStore->clearCache($cacheKey, $namespace); |
|
38
|
|
|
$this->cacheer->syncState(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->increment($cacheKey, ($amount * -1), $namespace); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function forever(string $cacheKey, mixed $cacheData): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
|
49
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function flushCache(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->cacheer->cacheStore->flushCache(); |
|
55
|
|
|
$this->cacheer->syncState(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
59
|
|
|
{ |
|
60
|
|
|
$cacheData = $this->cacheer->getCache($cacheKey, $namespace); |
|
61
|
|
|
|
|
62
|
|
|
if (!empty($cacheData) && is_numeric($cacheData)) { |
|
63
|
|
|
$this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
64
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): void |
|
72
|
|
|
{ |
|
73
|
|
|
$data = CacheerHelper::prepareForStorage($cacheData, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); |
|
74
|
|
|
$this->cacheer->cacheStore->putCache($cacheKey, $data, $namespace, $ttl); |
|
75
|
|
|
$this->cacheer->syncState(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function putMany(array $items, string $namespace = '', int $batchSize = 100): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->cacheer->cacheStore->putMany($items, $namespace, $batchSize); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = ''): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->cacheer->cacheStore->renewCache($cacheKey, $ttl, $namespace); |
|
86
|
|
|
$this->cacheer->syncState(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|