Passed
Push — main ( 72c6fa...9344f0 )
by Sílvio
02:59
created

DatabaseCacheStore::retrieveCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore;
4
5
use Silviooosilva\CacheerPhp\Interface\CacheerInterface;
6
use Silviooosilva\CacheerPhp\Helpers\CacheDatabaseHelper;
7
use Silviooosilva\CacheerPhp\Utils\CacheLogger;
8
use Silviooosilva\CacheerPhp\Repositories\CacheDatabaseRepository;
9
10
/**
11
 * Class DatabaseCacheStore
12
 * @author Sílvio Silva <https://github.com/silviooosilva>
13
 * @package Silviooosilva\CacheerPhp
14
 */
15
class DatabaseCacheStore implements CacheerInterface
16
{
17
    /**
18
     * @param boolean
19
     */
20
    private bool $success = false;
21
22
    /**
23
     * @param string
24
     */
25
    private string $message = '';
26
27
    /**
28
     * @var CacheLogger
29
     */
30
    private $logger = null;
31
32
    /**
33
     * @var CacheDatabaseRepository
34
     */
35
    private $cacheRepository;
36
37
    public function __construct(string $logPath)
38
    {
39
        $this->logger = new CacheLogger($logPath);
40
        $this->cacheRepository = new CacheDatabaseRepository();
41
    }
42
43
44
    /**
45
     * @param string $cacheKey
46
     * @param string $namespace
47
     * @param string|int $ttl
48
     * @return mixed
49
     */
50
    public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
51
    {
52
        $cacheData = $this->retrieveCache($cacheKey, $namespace);
53
        if ($cacheData) {
54
            $this->setMessage("Cache retrieved successfully", true);
55
            $this->logger->debug("{$this->getMessage()} from database driver.");
56
            return $cacheData;
57
        }
58
        $this->setMessage("CacheData not found, does not exists or expired", false);
59
        $this->logger->info("{$this->getMessage()} from database driver.");
60
        return null;
61
    }
62
    /**
63
     * @param string $cacheKey
64
     * @param mixed  $cacheData
65
     * @param string $namespace
66
     * @param string|int $ttl
67
     * @return bool
68
     */
69
    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600)
70
    {
71
        if($this->storeCache($cacheKey, $cacheData, $namespace, $ttl)){
72
            $this->logger->debug("{$this->getMessage()} from database driver.");
73
            return true;
74
        }
75
        $this->logger->error("{$this->getMessage()} from database driver.");
76
        return false;
77
    }
78
79
    /**
80
     * @param array   $items
81
     * @param string  $namespace
82
     * @param integer $batchSize
83
     * @return void
84
     */
85
    public function putMany(array $items, string $namespace = '', int $batchSize = 100)
86
    {
87
        $processedCount = 0;
88
        $itemCount = count($items);
89
        while ($processedCount < $itemCount) {
90
            $batchItems = array_slice($items, $processedCount, $batchSize);
91
            $this->processBatchItems($batchItems, $namespace);
92
            $processedCount += count($batchItems);
93
        }
94
    }
95
96
    /**
97
     * @param string $cacheKey
98
     * @param mixed  $cacheData
99
     * @param string $namespace
100
     * @return bool
101
     */
102
    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
103
    {
104
        $currentCacheData = $this->getCache($cacheKey, $namespace);
105
        $mergedCacheData = CacheDatabaseHelper::arrayIdentifier($currentCacheData, $cacheData);
106
107
        if ($this->updateCache($cacheKey, $mergedCacheData, $namespace)) {
108
            $this->logger->debug("{$this->getMessage()} from database driver.");
109
            return true;
110
        }
111
112
        $this->logger->error("{$this->getMessage()} from database driver.");
113
        return false;
114
    }
115
116
    /**
117
     * @param string $cacheKey
118
     * @param string $namespace
119
     * @return void
120
     */
121
    public function has(string $cacheKey, string $namespace = '')
122
    {
123
        $cacheData = $this->getCache($cacheKey, $namespace);
124
        if ($cacheData) {
125
            $this->logger->debug("Cache key: {$cacheKey} exists and it's available from database driver.");
126
        }
127
        $this->logger->warning("{$this->getMessage()} from database driver.");
128
    }
129
130
    /**
131
     * @param string $cacheKey
132
     * @param string $namespace
133
     * @param integer $ttl
134
     * @return void
135
     */
136
    public function renewCache(string $cacheKey, int | string $ttl, string $namespace = '')
137
    {
138
        $cacheData = $this->getCache($cacheKey, $namespace);
139
        if ($cacheData) {
140
            $this->renew($cacheKey, $ttl, $namespace);
141
            $this->setMessage("Cache with key {$cacheKey} renewed successfully", true);
142
            $this->logger->debug("{$this->getMessage()} from database driver.");
143
        }
144
    }
145
146
    /**
147
     * @param string $cacheKey
148
     * @param string $namespace
149
     * @return void
150
     */
151
    public function clearCache(string $cacheKey, string $namespace = '')
152
    {
153
        $data = $this->cacheRepository->clear($cacheKey, $namespace);
154
        if($data) {
155
            $this->setMessage("Cache deleted successfully!", true);
156
        } else {
157
            $this->setMessage("Cache does not exists!", false);
158
        }
159
160
        $this->logger->debug("{$this->getMessage()} from database driver.");
161
    }
162
163
    /**
164
     * @return void
165
     */
166
    public function flushCache()
167
    {
168
        if($this->cacheRepository->flush()){
169
            $this->setMessage("Flush finished successfully", true);
170
        } else {
171
            $this->setMessage("Something went wrong. Please, try again.", false);
172
        }
173
174
        $this->logger->info("{$this->getMessage()} from database driver.");
175
176
    }
177
178
179
    /**
180
     * @param string $cacheKey
181
     * @param string $namespace
182
     * @return mixed
183
     */
184
    private function retrieveCache(string $cacheKey, string $namespace = '')
185
    {
186
        return $this->cacheRepository->retrieve($cacheKey, $namespace);
187
    }
188
189
    /**
190
     * @param string $cacheKey
191
     * @param mixed  $cacheData
192
     * @param string $namespace
193
     * @param integer $ttl
194
     * @return bool
195
     */
196
    private function storeCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600)
197
    {
198
        $data = $this->cacheRepository->store($cacheKey, $cacheData, $namespace, $ttl);
199
        if($data) {
200
            $this->setMessage("Cache Stored Successfully", true);
201
            return true;
202
        }
203
        $this->setMessage("Already exists a cache with this key...", false);
204
        return false;
205
    }
206
207
208
    /**
209
     * @param string $cacheKey
210
     * @param mixed  $cacheData
211
     * @param string $namespace
212
     * @return bool
213
     */
214
    private function updateCache(string $cacheKey, mixed $cacheData, string $namespace = '')
215
    {
216
        $data = $this->cacheRepository->update($cacheKey, $cacheData, $namespace);
217
        if($data) {
218
            $this->setMessage("Cache updated successfully.", true);
219
            return true;
220
        }
221
        $this->setMessage("Cache does not exist or update failed!", false);
222
        return false;
223
    }
224
225
    /**
226
     * @param string $cacheKey
227
     * @param string|int $ttl
228
     * @param string $namespace
229
     * @return bool
230
     */
231
    private function renew(string $cacheKey, string|int $ttl = 3600, string $namespace = '')
232
    {
233
        $cacheData = $this->getCache($cacheKey, $namespace);
234
        if ($cacheData) {
235
            $renewedCache = $this->cacheRepository->renew($cacheKey, $ttl, $namespace);
236
            if ($renewedCache) {
237
                $this->setMessage("Cache with key {$cacheKey} renewed successfully", true);
238
                $this->logger->debug("{$this->getMessage()} from database driver.");
239
                return true;
240
            }
241
            return false;
242
        }
243
        return false;
244
    }
245
246
    /**
247
     * @param array  $batchItems
248
     * @param string $namespace
249
     * @return void
250
     */
251
    private function processBatchItems(array $batchItems, string $namespace)
252
    {
253
        foreach($batchItems as $item) {
254
            CacheDatabaseHelper::validateCacheItem($item);
255
            $cacheKey = $item['cacheKey'];
256
            $cacheData = $item['cacheData'];
257
            $mergedData = CacheDatabaseHelper::mergeCacheData($cacheData);
258
            $this->putCache($cacheKey, $mergedData, $namespace);
259
        }
260
    }
261
262
263
    /**
264
     * @param string  $message
265
     * @param boolean $success
266
     * @return void
267
     */
268
    private function setMessage(string $message, bool $success)
269
    {
270
        $this->message = $message;
271
        $this->success = $success;
272
    }
273
274
275
    /**
276
     * @return string
277
     */
278
    public function getMessage()
279
    {
280
        return $this->message;
281
    }
282
283
    /**
284
     * @return boolean
285
     */
286
    public function isSuccess()
287
    {
288
        return $this->success;
289
    }
290
}
291