1 <?php
2
3 namespace Alo\Cache;
4
5 use Redis;
6
7 if(!defined('GEN_START')) {
8 http_response_code(404);
9 die();
10 }
11
12 \Alo::loadConfig('redis');
13
14 /**
15 * A wrapper for PHP's Redis extension.
16 *
17 * @author Art <a.molcanovas@gmail.com>
18 * @package Cache
19 */
20 class RedisWrapper extends AbstractCache {
21
22 /**
23 * The memcached instance
24 *
25 * @var Redis
26 */
27 protected $client;
28
29 /**
30 * Instantiates the class
31 *
32 * @author Art <a.molcanovas@gmail.com>
33 *
34 * @param boolean $initialise_default_server Whether to add a server on construct
35 */
36 function __construct($initialise_default_server = true) {
37 $this->client = new Redis();
38 if($initialise_default_server) {
39 $this->addServer();
40 }
41 parent::__construct();
42
43 \Log::debug('RedisWrapper instantiated.');
44 }
45
46 /**
47 * Adds a server to the pool
48 *
49 * @author Art <a.molcanovas@gmail.com>
50 *
51 * @param string $ip The server IP
52 * @param int $port The server port
53 * @param int $weight The server's weight, ie how likely it is to be used. Currently unused by Redis.
54 *
55 * @return boolean
56 */
57 function addServer($ip = ALO_REDIS_IP, $port = ALO_REDIS_PORT, $weight = 1) {
58 \Log::debug('Added RedisWrapper server ' . $ip . ':' . $port);
59
60 return $this->client->connect($ip, $port);
61 }
62
63 /**
64 * Deletes a memcache key
65 *
66 * @author Art <a.molcanovas@gmail.com>
67 *
68 * @param string $key The key. Can supply multiple keys as arguments to delete them all.
69 *
70 * @return boolean
71 */
72 function delete($key) {
73 $this->client->delete(func_get_args());
74
75 return true;
76 }
77
78 /**
79 * Instantiates the class
80 *
81 * @author Art <a.molcanovas@gmail.com>
82 *
83 * @param boolean $initialise_default_server Whether to add a server on construct
84 *
85 * @return RedisWrapper
86 */
87 static function RedisWrapper($initialise_default_server = true) {
88 return new RedisWrapper($initialise_default_server);
89 }
90
91 /**
92 * Gets a cached value
93 *
94 * @author Art <a.molcanovas@gmail.com>
95 *
96 * @param string $id The value's key
97 *
98 * @return mixed
99 */
100 function get($id) {
101 $get = $this->client->get($id);
102 if(!$get) {
103 return null;
104 } else {
105 return unserialize($get);
106 }
107 }
108
109 /**
110 * Checks if Redis is available
111 *
112 * @author Art <a.molcanovas@gmail.com>
113 * @return boolean
114 */
115 static function is_available() {
116 return class_exists('\Redis');
117 }
118
119 /**
120 * Return all cached keys and values
121 *
122 * @author Art <a.molcanovas@gmail.com>
123 * @return array
124 */
125 function getAll() {
126 $keys = $this->client->keys('*');
127 $r = [];
128 if($keys) {
129 foreach($keys as $k) {
130 $r[$k] = $this->get($k);
131 }
132 }
133
134 return $r;
135 }
136
137 /**
138 * Clears all items from cache
139 *
140 * @author Art <a.molcanovas@gmail.com>
141 * @return boolean
142 */
143 function purge() {
144 return $this->client->flushAll();
145 }
146
147 /**
148 * Sets a cached key/value pair
149 *
150 * @author Art <a.molcanovas@gmail.com>
151 *
152 * @param string $key The key identifier
153 * @param mixed $var The value to set
154 * @param int $expire When to expire the set data. Defaults to 3600s.
155 *
156 * @return boolean
157 */
158 function set($key, $var, $expire = 3600) {
159 \Log::debug('Set the RedisWrapper key ' . $key);
160
161 return $this->client->setex($key, $expire, serialize($var));
162 }
163
164 }