|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
|
6
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CacheMutator |
|
10
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
11
|
|
|
* @package Silviooosilva\CacheerPhp |
|
12
|
|
|
*/ |
|
13
|
|
|
class CacheMutator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Cacheer |
|
17
|
|
|
*/ |
|
18
|
|
|
private Cacheer $cacheer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CacheMutator constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param Cacheer $cacheer |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Cacheer $cacheer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->cacheer = $cacheer; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Adds a cache item if it does not already exist. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $cacheKey |
|
34
|
|
|
* @param mixed $cacheData |
|
35
|
|
|
* @param string $namespace |
|
36
|
|
|
* @param int|string $ttl |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!empty($this->cacheer->getCache($cacheKey, $namespace))) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->putCache($cacheKey, $cacheData, $namespace, $ttl); |
|
46
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Appends data to an existing cache item. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $cacheKey |
|
55
|
|
|
* @param mixed $cacheData |
|
56
|
|
|
* @param string $namespace |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
|
60
|
|
|
{ |
|
61
|
|
|
$this->cacheer->cacheStore->appendCache($cacheKey, $cacheData, $namespace); |
|
62
|
|
|
$this->cacheer->syncState(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Clears a specific cache item. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $cacheKey |
|
69
|
|
|
* @param string $namespace |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function clearCache(string $cacheKey, string $namespace = '') |
|
73
|
|
|
{ |
|
74
|
|
|
$this->cacheer->cacheStore->clearCache($cacheKey, $namespace); |
|
75
|
|
|
$this->cacheer->syncState(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Decrements a numeric cache item by a specified amount. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $cacheKey |
|
82
|
|
|
* @param int $amount |
|
83
|
|
|
* @param string $namespace |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->increment($cacheKey, ($amount * -1), $namespace); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Checks if a cache item exists. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $cacheKey |
|
95
|
|
|
* @param string $namespace |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function forever(string $cacheKey, mixed $cacheData) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
|
101
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Flushes the entire cache. |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function flushCache() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->cacheer->cacheStore->flushCache(); |
|
112
|
|
|
$this->cacheer->syncState(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Gets a cache item by its key. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $cacheKey |
|
119
|
|
|
* @param string $namespace |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
123
|
|
|
{ |
|
124
|
|
|
$cacheData = $this->cacheer->getCache($cacheKey, $namespace); |
|
125
|
|
|
|
|
126
|
|
|
if (!empty($cacheData) && is_numeric($cacheData)) { |
|
127
|
|
|
$this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
128
|
|
|
$this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess()); |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Gets a cache item by its key. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $cacheKey |
|
139
|
|
|
* @param string $namespace |
|
140
|
|
|
* @param int|string $ttl |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
144
|
|
|
{ |
|
145
|
|
|
$data = CacheerHelper::prepareForStorage($cacheData, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); |
|
146
|
|
|
$this->cacheer->cacheStore->putCache($cacheKey, $data, $namespace, $ttl); |
|
147
|
|
|
$this->cacheer->syncState(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Puts multiple cache items in a batch. |
|
152
|
|
|
* |
|
153
|
|
|
* @param array $items |
|
154
|
|
|
* @param string $namespace |
|
155
|
|
|
* @param int $batchSize |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->cacheer->cacheStore->putMany($items, $namespace, $batchSize); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Renews the cache item with a new TTL. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $cacheKey |
|
167
|
|
|
* @param int|string $ttl |
|
168
|
|
|
* @param string $namespace |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
172
|
|
|
{ |
|
173
|
|
|
$this->cacheer->cacheStore->renewCache($cacheKey, $ttl, $namespace); |
|
174
|
|
|
$this->cacheer->syncState(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|