Passed
Push — master ( 71b418...90ddab )
by Stephen
02:32
created

RedisCache::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 TTL from the config.
13
     *
14
     * @return int
15
     */
16
    public static function ttl(): int
17
    {
18
        return config('redis-helpers.ttl', 3600);
19
    }
20
21
    /**
22
     * Get items from the cache.
23
     *
24
     * @param string $key
25
     * @return mixed
26
     */
27
    public static function get(string $key)
28
    {
29
        return Cache::get($key);
30
    }
31
32
    /**
33
     * Put items in the cache with a TTL.
34
     *
35
     * Use's environment's REDIS_KEY_EXPIRATION value if $expiration is null.
36
     *
37
     * @param string $key
38
     * @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...
39
     * @param int|null $expiration
40
     * @return bool
41
     */
42
    public static function set(string $key, $value = null, int $expiration = null): bool
43
    {
44
        // Store the $value in the Cache
45
        // todo: optimize expiration call
46
        return Cache::put(
47
            $key,
48
            $value,
49
            (isset($expiration) ? $expiration : self::ttl())
50
        );
51
    }
52
53
    /**
54
     * Put an array of key value pairs into the cache with a TTL.
55
     *
56
     * @param array $array
57
     * @param int|null $expiration
58
     * @return array
59
     */
60
    public static function setMany(array $array, int $expiration = null): array
61
    {
62
        foreach ($array as $key => $value) {
63
            self::set($key, $value, $expiration);
64
        }
65
66
        return array_values($array);
67
    }
68
69
    /**
70
     * Add a TTL attribute (time to live or time til expiration) to a Redis key.
71
     *
72
     * @param string $key
73
     * @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...
74
     * @return bool
75
     */
76
    public static function expire(string $key, $expiration = null): bool
77
    {
78
        // Use environment REDIS_KEY_EXPIRATION value if not set
79
        if (! $expiration) {
0 ignored issues
show
introduced by
$expiration is of type null, thus it always evaluated to false.
Loading history...
80
            $expiration = self::ttl();
81
        }
82
83
        // Create a key value pair with a null value and a TTL if the key is missing
84
        if (self::missing($key)) {
85
            return self::set($key, null, $expiration);
86
        }
87
88
        // Create a key value pair with original value and a TTL if the key exists
89
        else {
90
            return self::set($key, self::get($key), $expiration);
91
        }
92
    }
93
94
    /**
95
     * Delete Redis key's from the Cache.
96
     *
97
     * @param $keys array|string
98
     * @return array
99
     */
100
    public static function delete($keys): array
101
    {
102
        // Returns an array of deleted keys with success values
103
        return collect((array) $keys)
104
            ->mapWithKeys(function (string $key) {
105
                return [$key => Cache::forget($key)];
106
            })
107
            ->toArray();
108
    }
109
110
    /**
111
     * Determine if a redis key exists in the cache.
112
     *
113
     * @param string $key
114
     * @return bool
115
     */
116
    public static function exists(string $key): bool
117
    {
118
        return Cache::has($key);
119
    }
120
121
    /**
122
     * Determine if a redis key is missing from the cache.
123
     *
124
     * @param string $key
125
     * @return bool
126
     */
127
    public static function missing(string $key): bool
128
    {
129
        return Cache::missing($key);
130
    }
131
132
    /**
133
     * Create a Redis Key with a null value if it is missing.
134
     *
135
     * @param string $key
136
     * @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...
137
     * @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...
138
     * @return bool
139
     */
140
    public static function setIfMissing(string $key, $value = null, $expiration = null): bool
141
    {
142
        // Create the redis Key with an expiration
143
        if (self::missing($key)) {
144
            self::set($key, $value, $expiration);
145
146
            return true;
147
        }
148
149
        // Not created
150
        return false;
151
    }
152
153
    /**
154
     * Increment a Redis Key's value & return the new value.
155
     *
156
     * @param string $key
157
     * @param int $value
158
     * @param int|null $expiration
159
     * @return mixed
160
     */
161
    public static function increment(string $key, int $value = 1, int $expiration = null)
162
    {
163
        // Create the Key if it's missing
164
        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

164
        self::setIfMissing($key, 0, /** @scrutinizer ignore-type */ $expiration);
Loading history...
165
166
        // Increment the value & return the new value
167
        return Cache::increment($key, $value);
168
    }
169
170
    /**
171
     * Flush the entire redis cache.
172
     *
173
     * @return mixed
174
     */
175
    public static function flush()
176
    {
177
        return Cache::flush();
178
    }
179
180
    /**
181
     * Flush the redis cache of all keys with environment's prefix.
182
     *
183
     * @return array
184
     */
185
    public static function clear(): array
186
    {
187
        return self::delete('');
188
    }
189
190
    /**
191
     * Pass a $callback function to be stored in the Cache for an amount of time.
192
     *
193
     * @param string $key
194
     * @param int $ttl
195
     * @param Closure $callback
196
     * @return mixed
197
     */
198
    public static function remember(string $key, int $ttl, Closure $callback)
199
    {
200
        return Cache::remember($key, $ttl, $callback);
201
    }
202
203
    /**
204
     * Pass a $callback function to be stored in the Cache forever.
205
     *
206
     * @param string $key
207
     * @param Closure $callback
208
     * @return mixed
209
     */
210
    public static function rememberForever(string $key, Closure $callback)
211
    {
212
        return Cache::rememberForever($key, $callback);
213
    }
214
}
215