CacheMutator::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 10
rs 10
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): bool
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 bool
58
    */
59
    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = ''): bool
60
    {
61
        $this->cacheer->cacheStore->appendCache($cacheKey, $cacheData, $namespace);
62
        $this->cacheer->syncState();
63
64
        return $this->cacheer->isSuccess();
65
    }
66
67
    /**
68
    * Clears a specific cache item.
69
    *
70
    * @param string $cacheKey
71
    * @param string $namespace
72
    * @return bool
73
    */
74
    public function clearCache(string $cacheKey, string $namespace = ''): bool
75
    {
76
        $this->cacheer->cacheStore->clearCache($cacheKey, $namespace);
77
        $this->cacheer->syncState();
78
79
        return $this->cacheer->isSuccess();
80
    }
81
82
    /**
83
    * Decrements a numeric cache item by a specified amount.
84
    *
85
    * @param string $cacheKey
86
    * @param int $amount
87
    * @param string $namespace
88
    * @return bool
89
    */
90
    public function decrement(string $cacheKey, int $amount = 1, string $namespace = ''): bool
91
    {
92
        return $this->increment($cacheKey, ($amount * -1), $namespace);
93
    }
94
95
    /**
96
     * Checks if a cache item exists.
97
     *
98
     * @param string $cacheKey
99
     * @param mixed $cacheData
100
     * @return bool
101
     */
102
    public function forever(string $cacheKey, mixed $cacheData): bool
103
    {
104
        $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000);
105
        $this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess());
106
107
        return $this->cacheer->isSuccess();
108
    }
109
110
    /**
111
    * Flushes the entire cache.
112
    *
113
    * @return bool
114
    */
115
    public function flushCache(): bool
116
    {
117
        $this->cacheer->cacheStore->flushCache();
118
        $this->cacheer->syncState();
119
120
        return $this->cacheer->isSuccess();
121
    }
122
123
    /**
124
     * Gets a cache item by its key.
125
     *
126
     * @param string $cacheKey
127
     * @param int $amount
128
     * @param string $namespace
129
     * @return bool
130
     */
131
    public function increment(string $cacheKey, int $amount = 1, string $namespace = ''): bool
132
    {
133
        $cacheData = $this->cacheer->getCache($cacheKey, $namespace);
134
135
        if (!empty($cacheData) && is_numeric($cacheData)) {
136
            $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace);
137
            $this->cacheer->setInternalState($this->cacheer->getMessage(), $this->cacheer->isSuccess());
138
            return true;
139
        }
140
141
        return false;
142
    }
143
144
    /**
145
     * Gets a cache item by its key.
146
     *
147
     * @param string $cacheKey
148
     * @param mixed $cacheData
149
     * @param string $namespace
150
     * @param int|string $ttl
151
     * @return bool
152
     */
153
    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): bool
154
    {
155
        $data = CacheerHelper::prepareForStorage($cacheData, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey());
156
        $this->cacheer->cacheStore->putCache($cacheKey, $data, $namespace, $ttl);
157
        $this->cacheer->syncState();
158
159
        return $this->cacheer->isSuccess();
160
    }
161
162
    /**
163
    * Puts multiple cache items in a batch.
164
    *
165
    * @param array $items
166
    * @param string $namespace
167
    * @param int $batchSize
168
    * @return bool
169
    */
170
    public function putMany(array $items, string $namespace = '', int $batchSize = 100): bool
171
    {
172
        $this->cacheer->cacheStore->putMany($items, $namespace, $batchSize);
173
        $this->cacheer->syncState();
174
175
        return $this->cacheer->isSuccess();
176
    }
177
178
    /**
179
    * Renews the cache item with a new TTL.
180
    *
181
    * @param string $cacheKey
182
    * @param int|string $ttl
183
    * @param string $namespace
184
    * @return bool
185
    */
186
    public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = ''): bool
187
    {
188
        $this->cacheer->cacheStore->renewCache($cacheKey, $ttl, $namespace);
189
        $this->cacheer->syncState();
190
191
        return $this->cacheer->isSuccess();
192
    }
193
}
194