Passed
Push — master ( 71f2f4...c227b6 )
by Stephen
01:09 queued 10s
created

RedisCache::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Redis;
4
5
use Closure;
6
use Illuminate\Support\Facades\Cache;
7
use Sfneal\Actions\AbstractService;
8
9
class RedisCache extends AbstractService
10
{
11
    /**
12
     * Retrieve the Redis Key prefix from the config.
13
     *
14
     * @return string
15
     */
16
    public static function prefix(): string
17
    {
18
        return config('redis-helpers.prefix', 'app');
19
    }
20
21
    /**
22
     * Retrieve the Redis Key TTL from the config.
23
     *
24
     * @return int
25
     */
26
    public static function ttl(): int
27
    {
28
        return config('redis-helpers.ttl', 3600);
29
    }
30
31
    /**
32
     * Retrieve a formatted RedisKey with the environment prefix included.
33
     *
34
     * @param string $key
35
     * @return string
36
     */
37
    public static function key(string $key): string
38
    {
39
        return self::prefix().":$key";
40
    }
41
42
    /**
43
     * Get items from the cache.
44
     *
45
     * @param string $key
46
     * @return mixed
47
     */
48
    public static function get(string $key)
49
    {
50
        return Cache::get(self::key($key));
51
    }
52
53
    /**
54
     * Put items in the cache with a TTL.
55
     *
56
     * Use's environment's REDIS_KEY_EXPIRATION value if $expiration is null.
57
     *
58
     * @param string $key
59
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
60
     * @param int|null $expiration
61
     * @return bool
62
     */
63
    public static function set(string $key, $value = null, int $expiration = null): bool
64
    {
65
        // Store the $value in the Cache
66
        return Cache::put(
67
            self::key($key),
68
            $value,
69
            (isset($expiration) ? $expiration : self::ttl())
70
        );
71
    }
72
73
    /**
74
     * Put an array of key value pairs into the cache with a TTL.
75
     *
76
     * @param array $array
77
     * @param int|null $expiration
78
     * @return array
79
     */
80
    public static function setMany(array $array, int $expiration = null): array
81
    {
82
        foreach ($array as $key => $value) {
83
            self::set($key, $value, $expiration);
84
        }
85
86
        return array_values($array);
87
    }
88
89
    /**
90
     * Add a TTL attribute (time to live or time til expiration) to a Redis key.
91
     *
92
     * @param string $key
93
     * @param null $expiration
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $expiration is correct as it would always require null to be passed?
Loading history...
94
     * @return bool
95
     */
96
    public static function expire(string $key, $expiration = null): bool
97
    {
98
        // Use environment REDIS_KEY_EXPIRATION value if not set
99
        if (! $expiration) {
0 ignored issues
show
introduced by
$expiration is of type null, thus it always evaluated to false.
Loading history...
100
            $expiration = self::ttl();
101
        }
102
103
        // Create a key value pair with a null value and a TTL if the key is missing
104
        if (self::missing($key)) {
105
            return self::set($key, null, $expiration);
106
        }
107
108
        // Create a key value pair with original value and a TTL if the key exists
109
        else {
110
            return self::set($key, self::get($key), $expiration);
111
        }
112
    }
113
114
    /**
115
     * Delete Redis key's from the Cache.
116
     *
117
     * @param $keys array|string
118
     * @return array
119
     */
120
    public static function delete($keys): array
121
    {
122
        // Returns an array of deleted keys with success values
123
        return collect((array) $keys)
124
            ->mapWithKeys(function (string $key) {
125
                return [$key => Cache::forget(self::key($key))];
126
            })
127
            ->toArray();
128
    }
129
130
    /**
131
     * Determine if a redis key exists in the cache.
132
     *
133
     * @param string $key
134
     * @return bool
135
     */
136
    public static function exists(string $key): bool
137
    {
138
        return Cache::has(self::key($key));
139
    }
140
141
    /**
142
     * Determine if a redis key is missing from the cache.
143
     *
144
     * @param string $key
145
     * @return bool
146
     */
147
    public static function missing(string $key): bool
148
    {
149
        return Cache::missing(self::key($key));
150
    }
151
152
    /**
153
     * Create a Redis Key with a null value if it is missing.
154
     *
155
     * @param string $key
156
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
157
     * @param null $expiration
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $expiration is correct as it would always require null to be passed?
Loading history...
158
     * @return bool
159
     */
160
    public static function setIfMissing(string $key, $value = null, $expiration = null): bool
161
    {
162
        // Create the redis Key with an expiration
163
        if (self::missing($key)) {
164
            self::set($key, $value, $expiration);
165
166
            return true;
167
        }
168
169
        // Not created
170
        return false;
171
    }
172
173
    /**
174
     * Increment a Redis Key's value & return the new value.
175
     *
176
     * @param string $key
177
     * @param int $value
178
     * @param int|null $expiration
179
     * @return mixed
180
     */
181
    public static function increment(string $key, int $value = 1, int $expiration = null)
182
    {
183
        // Create the Key if it's missing
184
        self::setIfMissing($key, 0, $expiration);
0 ignored issues
show
Bug introduced by
It seems like $expiration can also be of type integer; however, parameter $expiration of Sfneal\Helpers\Redis\RedisCache::setIfMissing() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
        self::setIfMissing($key, 0, /** @scrutinizer ignore-type */ $expiration);
Loading history...
185
186
        // Increment the value & return the new value
187
        return Cache::increment(self::key($key), $value);
188
    }
189
190
    /**
191
     * Flush the entire redis cache.
192
     *
193
     * @return mixed
194
     */
195
    public static function flush()
196
    {
197
        return Cache::flush();
198
    }
199
200
    /**
201
     * Flush the redis cache of all keys with environment's prefix.
202
     *
203
     * @return mixed
204
     */
205
    public static function clear()
206
    {
207
        return self::delete('');
208
    }
209
210
    /**
211
     * Pass a $callback function to be stored in the Cache for an amount of time.
212
     *
213
     * @param string $key
214
     * @param int $ttl
215
     * @param Closure $callback
216
     * @return mixed
217
     */
218
    public static function remember(string $key, int $ttl, Closure $callback)
219
    {
220
        return Cache::remember($key, $ttl, $callback);
221
    }
222
223
    /**
224
     * Pass a $callback function to be stored in the Cache forever.
225
     *
226
     * @param string $key
227
     * @param Closure $callback
228
     * @return mixed
229
     */
230
    public static function rememberForever(string $key, Closure $callback)
231
    {
232
        return Cache::rememberForever($key, $callback);
233
    }
234
}
235