|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
|
7
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; |
|
8
|
|
|
use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CacheRetriever |
|
12
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
13
|
|
|
* @package Silviooosilva\CacheerPhp |
|
14
|
|
|
*/ |
|
15
|
|
|
class CacheRetriever |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Cacheer |
|
19
|
|
|
*/ |
|
20
|
|
|
private Cacheer $cacheer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CacheRetriever constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Cacheer $cacheer |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Cacheer $cacheer) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->cacheer = $cacheer; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Retrieves a cache item by its key. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $cacheKey |
|
36
|
|
|
* @param string $namespace |
|
37
|
|
|
* @param int|string $ttl |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600) |
|
41
|
|
|
{ |
|
42
|
|
|
$cacheData = $this->cacheer->cacheStore->getCache($cacheKey, $namespace, $ttl); |
|
43
|
|
|
$this->cacheer->syncState(); |
|
44
|
|
|
|
|
45
|
|
|
if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { |
|
46
|
|
|
$cacheData = CacheerHelper::recoverFromStorage($cacheData, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->cacheer->isFormatted() ? new CacheDataFormatter($cacheData) : $cacheData; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retrieves multiple cache items by their keys. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $cacheKeys |
|
56
|
|
|
* @param string $namespace |
|
57
|
|
|
* @param int|string $ttl |
|
58
|
|
|
* @return array|CacheDataFormatter |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600) |
|
61
|
|
|
{ |
|
62
|
|
|
$cachedData = $this->cacheer->cacheStore->getMany($cacheKeys, $namespace, $ttl); |
|
63
|
|
|
$this->cacheer->syncState(); |
|
64
|
|
|
|
|
65
|
|
|
if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { |
|
66
|
|
|
foreach ($cachedData as &$data) { |
|
67
|
|
|
$data = CacheerHelper::recoverFromStorage($data, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->cacheer->isFormatted() ? new CacheDataFormatter($cachedData) : $cachedData; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieves all cache items in a namespace. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $namespace |
|
78
|
|
|
* @return CacheDataFormatter|mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getAll(string $namespace = '') |
|
81
|
|
|
{ |
|
82
|
|
|
$cachedData = $this->cacheer->cacheStore->getAll($namespace); |
|
83
|
|
|
$this->cacheer->syncState(); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { |
|
86
|
|
|
foreach ($cachedData as &$data) { |
|
87
|
|
|
$data = CacheerHelper::recoverFromStorage($data, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->cacheer->isFormatted() ? new CacheDataFormatter($cachedData) : $cachedData; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retrieves a cache item, deletes it, and returns its data. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $cacheKey |
|
98
|
|
|
* @param string $namespace |
|
99
|
|
|
* @return mixed|null |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getAndForget(string $cacheKey, string $namespace = '') |
|
102
|
|
|
{ |
|
103
|
|
|
$cachedData = $this->getCache($cacheKey, $namespace); |
|
104
|
|
|
|
|
105
|
|
|
if (!empty($cachedData)) { |
|
106
|
|
|
$this->cacheer->setInternalState("Cache retrieved and deleted successfully!", true); |
|
107
|
|
|
$this->cacheer->clearCache($cacheKey, $namespace); |
|
108
|
|
|
return $cachedData; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Retrieves a cache item, or executes a callback to store it if not found. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $cacheKey |
|
118
|
|
|
* @param int|string $ttl |
|
119
|
|
|
* @param Closure $callback |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
public function remember(string $cacheKey, int|string $ttl, Closure $callback) |
|
123
|
|
|
{ |
|
124
|
|
|
$cachedData = $this->getCache($cacheKey, ttl: $ttl); |
|
125
|
|
|
|
|
126
|
|
|
if (!empty($cachedData)) { |
|
127
|
|
|
return $cachedData; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$cacheData = $callback(); |
|
131
|
|
|
$this->cacheer->putCache($cacheKey, $cacheData, ttl: $ttl); |
|
132
|
|
|
return $cacheData; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Retrieves a cache item indefinitely, or executes a callback to store it if not found. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $cacheKey |
|
139
|
|
|
* @param Closure $callback |
|
140
|
|
|
* @return mixed |
|
141
|
|
|
*/ |
|
142
|
|
|
public function rememberForever(string $cacheKey, Closure $callback) |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->remember($cacheKey, 31536000 * 1000, $callback); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Checks if a cache item exists. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $cacheKey |
|
151
|
|
|
* @param string $namespace |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function has(string $cacheKey, string $namespace = '') |
|
155
|
|
|
{ |
|
156
|
|
|
$this->cacheer->cacheStore->has($cacheKey, $namespace); |
|
157
|
|
|
$this->cacheer->syncState(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|