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 * Instantiates the class
22 *
23 * @author Art <a.molcanovas@gmail.com>
24 */
25 function __construct() {
26 if (!Alo::$cache) {
27 Alo::$cache = &$this;
28 }
29 }
30
31 /**
32 * Key getter
33 *
34 * @author Art <a.molcanovas@gmail.com>
35 * @param string $key The key
36 * @return mixed
37 */
38 function __get($key) {
39 return $this->get($key);
40 }
41
42 /**
43 * Sets a value with its default expiration time
44 *
45 * @author Art <a.molcanovas@gmail.com>
46 * @param string $key The key
47 * @param mixed $val The value
48 * @return bool
49 */
50 function __set($key, $val) {
51 return $this->set($key, $val);
52 }
53
54 /**
55 * Checks if a key is set in cache
56 *
57 * @author Art <a.molcanovas@gmail.com>
58 * @param string $key The key
59 * @return bool
60 */
61 function __isset($key) {
62 return $this->get($key) ? true : false;
63 }
64
65 /**
66 * Removes a key from cache
67 *
68 * @author Art <a.molcanovas@gmail.com>
69 * @param string $key The key
70 */
71 function __unset($key) {
72 $this->delete($key);
73 }
74
75 /**
76 * Checks if a caching extension is available
77 *
78 * @author Art <a.molcanovas@gmail.com>
79 * @return boolean
80 */
81 static function is_available() {
82 return class_exists('\Memcached') || class_exists('\Memcache');
83 }
84
85 /**
86 * Clears all items from cache
87 *
88 * @author Art <a.molcanovas@gmail.com>
89 * @return boolean
90 */
91 abstract function purge();
92
93 /**
94 * Adds a server to the pool
95 *
96 * @author Art <a.molcanovas@gmail.com>
97 * @param string $ip The server IP
98 * @param string $port The server port
99 * @param int $weight The server's weight, ie how likely it is to be used
100 * @return boolean
101 */
102 abstract function addServer($ip, $port, $weight);
103
104 /**
105 * Gets cache process info
106 *
107 * @author Art <a.molcanovas@gmail.com>
108 * @return array
109 */
110 abstract function getStats();
111
112 /**
113 * Deletes a memcache key
114 *
115 * @author Art <a.molcanovas@gmail.com>
116 * @param string $key The key
117 * @return boolean
118 */
119 abstract function delete($key);
120
121 /**
122 * Gets a cached value
123 *
124 * @author Art <a.molcanovas@gmail.com>
125 * @param string $id The value's key
126 * @return mixed
127 */
128 abstract function get($id);
129
130 /**
131 * Return all cached keys and values
132 *
133 * @author Art <a.molcanovas@gmail.com>
134 * @return array
135 */
136 abstract function getAll();
137
138 /**
139 * Sets a cached key/value pair
140 *
141 * @author Art <a.molcanovas@gmail.com>
142 * @param string $key The key identifier
143 * @param mixed $var The value to set
144 * @param int $expire When to expire the set data. Defaults to 3600s.
145 * @return boolean
146 */
147 abstract function set($key, $var, $expire = 3600);
148
149 /**
150 * Deletes all cached entries with the supplied prefix
151 *
152 * @author Art <a.molcanovas@gmail.com>
153 * @param string $prefix The prefix
154 * @return AbstractCache
155 */
156 function deleteWithPrefix($prefix) {
157 $length = strlen($prefix);
158 $entries = array_keys($this->getAll());
159
160 \Log::debug('Deleting all cache entries with prefix ' . $prefix);
161 foreach ($entries as $key) {
162 if (substr($key, 0, $length) == $prefix) {
163 $this->delete($key);
164 }
165 }
166
167 return $this;
168 }
169
170 /**
171 * Deletes all cached entries with the supplied suffix
172 *
173 * @author Art <a.molcanovas@gmail.com>
174 * @param string $suffix The suffix
175 * @return AbstractCache
176 */
177 function deleteWithSuffix($suffix) {
178 $length = strlen($suffix) * -1;
179 $entries = array_keys($this->getAll());
180
181 \Log::debug('Deleting all cache entries with suffix ' . $suffix);
182 foreach ($entries as $key) {
183 if (substr($key, $length) == $suffix) {
184 $this->delete($key);
185 }
186 }
187
188 return $this;
189 }
190
191 }