CacheRetriever::getCachedDatum()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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