1: <?php
2: namespace Opencart\System\Library\Cache;
3: /**
4: * Class Redis
5: *
6: * @package Opencart\System\Library\Cache
7: */
8: class Redis {
9: /**
10: * @var \Redis
11: */
12: private \Redis $redis;
13: /**
14: * @var int
15: */
16: private int $expire;
17:
18: /**
19: * Constructor
20: *
21: * @param int $expire
22: */
23: public function __construct(int $expire = 3600) {
24: $this->expire = $expire;
25:
26: $this->redis = new \Redis();
27: $this->redis->pconnect(CACHE_HOSTNAME, CACHE_PORT);
28: }
29:
30: /**
31: * Get
32: *
33: * @param string $key
34: *
35: * @return mixed
36: */
37: public function get(string $key) {
38: $data = $this->redis->get(CACHE_PREFIX . $key);
39:
40: return json_decode($data, true);
41: }
42:
43: /**
44: * Set
45: *
46: * @param string $key
47: * @param mixed $value
48: * @param int $expire
49: *
50: * @return void
51: */
52: public function set(string $key, $value, int $expire = 0): void {
53: if (!$expire) {
54: $expire = $this->expire;
55: }
56:
57: $status = $this->redis->set(CACHE_PREFIX . $key, json_encode($value));
58:
59: if ($status) {
60: $this->redis->expire(CACHE_PREFIX . $key, $expire);
61: }
62: }
63:
64: /**
65: * Delete
66: *
67: * @param string $key
68: *
69: * @return void
70: */
71: public function delete(string $key): void {
72: $this->redis->del(CACHE_PREFIX . $key);
73: }
74: }
75: