1 <?php
2
3 namespace Alo\Cache;
4
5 use Alo;
6
7 if(!defined('GEN_START')) {
8 http_response_code(404);
9 die();
10 }
11
12 /**
13 * The abstract cache class
14 *
15 * @author Art <a.molcanovas@gmail.com>
16 * @package Cache
17 */
18 abstract class AbstractCache {
19
20 /**
21 * Classes to check in "is_available()"
22 *
23 * @var array
24 */
25 private static $classes = ['Memcache', 'Memcached', 'Redis'];
26 /**
27 * The abstract client
28 *
29 * @var \Redis|\Memcache|\Memcached
30 */
31 protected $client;
32
33 /**
34 * Instantiates the class
35 *
36 * @author Art <a.molcanovas@gmail.com>
37 */
38 function __construct() {
39 if(!Alo::$cache) {
40 Alo::$cache = &$this;
41 }
42 }
43
44 /**
45 * Checks if a caching extension is available
46 *
47 * @author Art <a.molcanovas@gmail.com>
48 * @return boolean
49 */
50 static function is_available() {
51 foreach(self::$classes as $class) {
52 if(class_exists('\\' . $class)) {
53 return true;
54 }
55 }
56
57 return false;
58 }
59
60 /**
61 * Calls a method of the caching client
62 *
63 * @author Art <a.molcanovas@gmail.com>
64 *
65 * @param string $method The method
66 * @param array $args Method args
67 *
68 * @return mixed Whatever the method returns
69 */
70 function __call($method, $args) {
71 return call_user_func_array([$this->client, $method], $args);
72 }
73
74 /**
75 * Key getter
76 *
77 * @author Art <a.molcanovas@gmail.com>
78 *
79 * @param string $key The key
80 *
81 * @return mixed
82 */
83 function __get($key) {
84 return $this->get($key);
85 }
86
87 /**
88 * Sets a value with its default expiration time
89 *
90 * @author Art <a.molcanovas@gmail.com>
91 *
92 * @param string $key The key
93 * @param mixed $val The value
94 *
95 * @return bool
96 */
97 function __set($key, $val) {
98 return $this->set($key, $val);
99 }
100
101 /**
102 * Gets a cached value
103 *
104 * @author Art <a.molcanovas@gmail.com>
105 *
106 * @param string $id The value's key
107 *
108 * @return mixed
109 */
110 abstract function get($id);
111
112 /**
113 * Sets a cached key/value pair
114 *
115 * @author Art <a.molcanovas@gmail.com>
116 *
117 * @param string $key The key identifier
118 * @param mixed $var The value to set
119 * @param int $expire When to expire the set data. Defaults to 3600s.
120 *
121 * @return boolean
122 */
123 abstract function set($key, $var, $expire = 3600);
124
125 /**
126 * Checks if a key is set in cache
127 *
128 * @author Art <a.molcanovas@gmail.com>
129 *
130 * @param string $key The key
131 *
132 * @return bool
133 */
134 function __isset($key) {
135 return $this->get($key) ? true : false;
136 }
137
138 /**
139 * Removes a key from cache
140 *
141 * @author Art <a.molcanovas@gmail.com>
142 *
143 * @param string $key The key
144 */
145 function __unset($key) {
146 $this->delete($key);
147 }
148
149 /**
150 * Deletes a memcache key
151 *
152 * @author Art <a.molcanovas@gmail.com>
153 *
154 * @param string $key The key
155 *
156 * @return boolean
157 */
158 abstract function delete($key);
159
160 /**
161 * Clears all items from cache
162 *
163 * @author Art <a.molcanovas@gmail.com>
164 * @return boolean
165 */
166 abstract function purge();
167
168 /**
169 * Adds a server to the pool
170 *
171 * @author Art <a.molcanovas@gmail.com>
172 *
173 * @param string $ip The server IP
174 * @param int $port The server port
175 * @param int $weight The server's weight, ie how likely it is to be used
176 *
177 * @return boolean
178 */
179 abstract function addServer($ip, $port, $weight);
180
181 /**
182 * Deletes all cached entries with the supplied prefix
183 *
184 * @author Art <a.molcanovas@gmail.com>
185 *
186 * @param string $prefix The prefix
187 *
188 * @return AbstractCache
189 */
190 function deleteWithPrefix($prefix) {
191 $length = strlen($prefix);
192 $entries = array_keys($this->getAll());
193
194 \Log::debug('Deleting all cache entries with prefix ' . $prefix);
195 foreach($entries as $key) {
196 if(substr($key, 0, $length) == $prefix) {
197 $this->delete($key);
198 }
199 }
200
201 return $this;
202 }
203
204 /**
205 * Return all cached keys and values
206 *
207 * @author Art <a.molcanovas@gmail.com>
208 * @return array
209 */
210 abstract function getAll();
211
212 /**
213 * Deletes all cached entries with the supplied suffix
214 *
215 * @author Art <a.molcanovas@gmail.com>
216 *
217 * @param string $suffix The suffix
218 *
219 * @return AbstractCache
220 */
221 function deleteWithSuffix($suffix) {
222 $length = strlen($suffix) * -1;
223 $entries = array_keys($this->getAll());
224
225 \Log::debug('Deleting all cache entries with suffix ' . $suffix);
226 foreach($entries as $key) {
227 if(substr($key, $length) == $suffix) {
228 $this->delete($key);
229 }
230 }
231
232 return $this;
233 }
234
235 }