Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.68% covered (success)
92.68%
38 / 41
80.00% covered (success)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Redis
92.68% covered (success)
92.68%
38 / 41
80.00% covered (success)
80.00%
8 / 10
18.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 redis
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItemTtl
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 saveItem
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getItem
88.89% covered (success)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 hasItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 deleteItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 destroy
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Cache\Adapter;
15
16/**
17 * Redis cache adapter class
18 *
19 * @category   Pop
20 * @package    Pop\Cache
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26class Redis extends AbstractAdapter
27{
28
29    /**
30     * Redis object
31     * @var ?\Redis
32     */
33    protected ?\Redis $redis = null;
34
35    /**
36     * Constructor
37     *
38     * Instantiate the memcache cache object
39     *
40     * @param  int    $ttl
41     * @param  string $host
42     * @param  int    $port
43     * @throws Exception
44     */
45    public function __construct(int $ttl = 0, string $host = 'localhost', int $port = 6379)
46    {
47        parent::__construct($ttl);
48        if (!class_exists('Redis', false)) {
49            throw new Exception('Error: Redis is not available.');
50        }
51
52        $this->redis = new \Redis();
53        if (!$this->redis->connect($host, (int)$port)) {
54            throw new Exception('Error: Unable to connect to the redis server.');
55        }
56    }
57
58    /**
59     * Get the redis object.
60     *
61     * @return \Redis
62     */
63    public function redis(): \Redis
64    {
65        return $this->redis;
66    }
67
68    /**
69     * Get the current version of redis.
70     *
71     * @return string
72     */
73    public function getVersion(): string
74    {
75        return $this->redis->info()['redis_version'];
76    }
77
78    /**
79     * Get the time-to-live for an item in cache
80     *
81     * @param  string $id
82     * @return int
83     */
84    public function getItemTtl(string $id): int
85    {
86        $cacheValue = $this->redis->get($id);
87        $ttl        = false;
88
89        if ($cacheValue !== false) {
90            $cacheValue = unserialize($cacheValue);
91            $ttl      = $cacheValue['ttl'];
92        }
93
94        return $ttl;
95    }
96
97    /**
98     * Save an item to cache
99     *
100     * @param  string $id
101     * @param  mixed  $value
102     * @param  ?int   $ttl
103     * @return Redis
104     */
105    public function saveItem(string $id, mixed $value, ?int $ttl = null): Redis
106    {
107        $cacheValue = [
108            'start' => time(),
109            'ttl'   => ($ttl !== null) ? $ttl : $this->ttl,
110            'value' => $value
111        ];
112
113        if ($cacheValue['ttl'] != 0) {
114            $this->redis->set($id, serialize($cacheValue), $cacheValue['ttl']);
115        } else {
116            $this->redis->set($id, serialize($cacheValue));
117        }
118        return $this;
119    }
120
121    /**
122     * Get an item from cache
123     *
124     * @param  string $id
125     * @return mixed
126     */
127    public function getItem(string $id): mixed
128    {
129        $cacheValue = $this->redis->get($id);
130        $value      = false;
131
132        if ($cacheValue !== false) {
133            $cacheValue = unserialize($cacheValue);
134            if ((($cacheValue['ttl'] == 0) || ((time() - $cacheValue['start']) <= $cacheValue['ttl']))) {
135                $value = $cacheValue['value'];
136            } else {
137                $this->deleteItem($id);
138            }
139        } else {
140            $this->deleteItem($id);
141        }
142
143        return $value;
144    }
145
146    /**
147     * Determine if the item exist in cache
148     *
149     * @param  string $id
150     * @return bool
151     */
152    public function hasItem(string $id): bool
153    {
154        $cacheValue = $this->getItem($id);
155        return ($cacheValue !== false);
156    }
157
158    /**
159     * Delete a value in cache
160     *
161     * @param  string $id
162     * @return Redis
163     */
164    public function deleteItem(string $id): Redis
165    {
166        $this->redis->del($id);
167        return $this;
168    }
169
170    /**
171     * Clear all stored values from cache
172     *
173     * @return Redis
174     */
175    public function clear(): Redis
176    {
177        $this->redis->flushDb();
178        return $this;
179    }
180
181    /**
182     * Destroy cache resource
183     *
184     * @return Redis
185     */
186    public function destroy(): Redis
187    {
188        $this->redis->flushDb();
189        $this->redis = null;
190        return $this;
191    }
192
193}