Passed
Pull Request — main (#7)
by Daryl
02:29
created

RedisCache::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Clubdeuce\Tessitura\Cache;
4
5
use Clubdeuce\Tessitura\Interfaces\CacheInterface;
6
use Predis\Client;
7
use Exception;
8
9
class RedisCache implements CacheInterface
10
{
11
    private Client $redis;
12
13
    public function __construct(Client $redis)
14
    {
15
        $this->redis = $redis;
16
    }
17
18
    /**
19
     * Get a value from the cache.
20
     *
21
     * @param string $key The cache key
22
     * @return mixed|null The cached value or null if not found
23
     */
24
    public function get(string $key): mixed
25
    {
26
        try {
27
            $value = $this->redis->get($key);
28
            if ($value === null) {
29
                return null;
30
            }
31
            return json_decode($value, true);
32
        } catch (Exception) {
33
            return null;
34
        }
35
    }
36
37
    /**
38
     * Set a value in the cache.
39
     *
40
     * @param string $key The cache key
41
     * @param mixed $value The value to cache
42
     * @param int $ttl Time to live in seconds
43
     * @return bool True on success, false on failure
44
     */
45
    public function set(string $key, mixed $value, int $ttl = 3600): bool
46
    {
47
        try {
48
            $serializedValue = json_encode($value);
49
            if ($ttl > 0) {
50
                return $this->redis->setex($key, $ttl, $serializedValue) === 'OK';
51
            } else {
52
                return $this->redis->set($key, $serializedValue) === 'OK';
53
            }
54
        } catch (Exception) {
55
            return false;
56
        }
57
    }
58
59
    /**
60
     * Check if a key exists in the cache.
61
     *
62
     * @param string $key The cache key
63
     * @return bool True if the key exists, false otherwise
64
     */
65
    public function has(string $key): bool
66
    {
67
        try {
68
            return $this->redis->exists($key) > 0;
69
        } catch (Exception) {
70
            return false;
71
        }
72
    }
73
74
    /**
75
     * Delete a value from the cache.
76
     *
77
     * @param string $key The cache key
78
     * @return bool True on success, false on failure
79
     */
80
    public function delete(string $key): bool
81
    {
82
        try {
83
            return $this->redis->del($key) > 0;
84
        } catch (Exception) {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * Clear all cached values.
91
     *
92
     * @return bool True on success, false on failure
93
     */
94
    public function clear(): bool
95
    {
96
        try {
97
            return $this->redis->flushdb() === 'OK';
98
        } catch (Exception) {
99
            return false;
100
        }
101
    }
102
}