CacheRetriever::getAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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