Passed
Push — main ( 220b50...55f6d1 )
by Daryl
03:00
created

RedisCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 30
c 2
b 0
f 0
dl 0
loc 92
ccs 16
cts 28
cp 0.5714
rs 10

6 Methods

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