Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 481 lines (430 sloc) 12.74 kB
79fbde3 @thousandsofthem First version
thousandsofthem authored
1 <?php
2 /**
3 * PHP client for IronCache
4 *
5 * @link https://github.com/iron-io/iron_cache_php
6 * @link http://www.iron.io/products/cache
7 * @link http://dev.iron.io/
3a4ee18 @thousandsofthem bump version
thousandsofthem authored
8 * @version 0.1.3
79fbde3 @thousandsofthem First version
thousandsofthem authored
9 * @package IronCache
10 * @copyright Feel free to copy, steal, take credit for, or whatever you feel like doing with this code. ;)
11 */
12
13
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
14 class IronCache_Item
15 {
3de113b @Sam152 Updated class to use "protected" instead of "private" access modifier.
Sam152 authored
16 protected $value;
17 protected $expires_in;
18 protected $replace;
19 protected $add;
79fbde3 @thousandsofthem First version
thousandsofthem authored
20
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
21 const MAX_EXPIRES_IN = 2592000;
79fbde3 @thousandsofthem First version
thousandsofthem authored
22
23 /**
24 * Create a new item.
25 *
26 * @param array|string $item
27 * An array of item properties or a string of the item value.
28 * Fields in item array:
29 * Required:
30 * - value: string - The item data, as a string.
31 * Optional:
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
32 * - expires_in: integer - How long in seconds to keep the item on the cache before it is deleted.
33 * Default is 604,800 seconds (7 days). Maximum is 2,592,000 seconds (30 days).
79fbde3 @thousandsofthem First version
thousandsofthem authored
34 * - replace: boolean - Will only work if key already exists.
35 * - add: boolean - Will only work if key does not exist.
36 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
37 public function __construct($item)
38 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
39 if (is_string($item) || is_integer($item)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
40 $this->setValue($item);
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
41 } elseif (is_array($item)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
42 $this->setValue($item['value']);
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
43 if (array_key_exists("replace", $item)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
44 $this->setReplace($item['replace']);
45 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
46 if (array_key_exists("add", $item)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
47 $this->setAdd($item['add']);
48 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
49 if (array_key_exists("expires_in", $item)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
50 $this->setExpiresIn($item['expires_in']);
51 }
52 }
53 }
54
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
55 public function setValue($value)
56 {
fb6c61b @thousandsofthem #5 allow "0" as value
thousandsofthem authored
57 if ($value === null) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
58 throw new InvalidArgumentException("Please specify a value");
59 } else {
60 $this->value = $value;
61 }
62 }
63
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
64 public function getValue()
65 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
66 return $this->value;
67 }
68
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
69 public function setReplace($replace)
70 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
71 $this->replace = $replace;
72 }
73
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
74 public function getReplace()
75 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
76 return $this->replace;
77 }
78
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
79 public function setAdd($add)
80 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
81 $this->add = $add;
82 }
83
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
84 public function getAdd()
85 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
86 return $this->add;
87 }
88
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
89 public function setExpiresIn($expires_in)
90 {
91 if ($expires_in > self::MAX_EXPIRES_IN) {
92 throw new InvalidArgumentException("Expires In can't be greater than ".self::MAX_EXPIRES_IN.".");
79fbde3 @thousandsofthem First version
thousandsofthem authored
93 } else {
94 $this->expires_in = $expires_in;
95 }
96 }
97
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
98 public function getExpiresIn()
99 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
100 return $this->expires_in;
101 }
102
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
103 public function asArray()
104 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
105 $array = array();
106 $array['value'] = $this->getValue();
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
107 if ($this->getExpiresIn() != null) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
108 $array['expires_in'] = $this->getExpiresIn();
109 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
110 if ($this->getReplace() != null) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
111 $array['replace'] = $this->getReplace();
112 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
113 if ($this->getAdd() != null) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
114 $array['add'] = $this->getAdd();
115 }
116 return $array;
117 }
118 }
119
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
120 class IronCache extends IronCore
121 {
3a4ee18 @thousandsofthem bump version
thousandsofthem authored
122 protected $client_version = '0.1.3';
79fbde3 @thousandsofthem First version
thousandsofthem authored
123 protected $client_name = 'iron_cache_php';
124 protected $product_name = 'iron_cache';
125 protected $default_values = array(
126 'protocol' => 'https',
127 'host' => 'cache-aws-us-east-1.iron.io',
128 'port' => '443',
129 'api_version' => '1',
130 );
131
3de113b @Sam152 Updated class to use "protected" instead of "private" access modifier.
Sam152 authored
132 protected $cache_name;
79fbde3 @thousandsofthem First version
thousandsofthem authored
133
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
134 public $session_expire_time = 172800; # 2 days
135
79fbde3 @thousandsofthem First version
thousandsofthem authored
136 /**
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
137 * @param string|array $config
79fbde3 @thousandsofthem First version
thousandsofthem authored
138 * Array of options or name of config file.
139 * Fields in options array or in config:
140 *
141 * Required:
142 * - token
143 * - project_id
144 * Optional:
145 * - protocol
146 * - host
147 * - port
148 * - api_version
149 * @param string|null $cache_name set default cache name
150 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
151 public function __construct($config = null, $cache_name = null)
152 {
153 $this->getConfigData($config);
79fbde3 @thousandsofthem First version
thousandsofthem authored
154 $this->url = "{$this->protocol}://{$this->host}:{$this->port}/{$this->api_version}/";
155 $this->setCacheName($cache_name);
156 }
157
158 /**
159 * Switch active project
160 *
161 * @param string $project_id Project ID
162 * @throws InvalidArgumentException
163 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
164 public function setProjectId($project_id)
165 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
166 if (!empty($project_id)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
167 $this->project_id = $project_id;
168 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
169 if (empty($this->project_id)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
170 throw new InvalidArgumentException("Please set project_id");
171 }
172 }
173
174 /**
175 * Set default cache name
176 *
177 * @param string $cache_name name of cache
178 * @throws InvalidArgumentException
179 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
180 public function setCacheName($cache_name)
181 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
182 if (!empty($cache_name)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
183 $this->cache_name = $cache_name;
184 }
185
186 }
187
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
188 public function getCaches($page = 0)
189 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
190 $url = "projects/{$this->project_id}/caches";
191 $params = array();
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
192 if ($page > 0) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
193 $params['page'] = $page;
194 }
195 $this->setJsonHeaders();
196 return self::json_decode($this->apiCall(self::GET, $url, $params));
197 }
198
199 /**
200 * Get information about cache.
201 * Also returns cache size.
202 *
203 * @param string $cache
204 * @return mixed
205 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
206 public function getCache($cache)
207 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
208 $cache = self::encodeCache($cache);
209 $url = "projects/{$this->project_id}/caches/$cache";
210 $this->setJsonHeaders();
211 return self::json_decode($this->apiCall(self::GET, $url));
212 }
213
214 /**
215 * Push a item on the cache at 'key'
216 *
217 * Examples:
218 * <code>
fb10436 @janpieper corrected method name (postItem --> putItem)
janpieper authored
219 * $cache->putItem("test_cache", 'default', "Hello world");
79fbde3 @thousandsofthem First version
thousandsofthem authored
220 * </code>
221 * <code>
7c8227f @thousandsofthem fixing small bugs
thousandsofthem authored
222 * $cache->putItem("test_cache", 'default', array(
79fbde3 @thousandsofthem First version
thousandsofthem authored
223 * "value" => "Test Item",
224 * 'expires_in' => 2*24*3600, # 2 days
225 * "replace" => true
226 * ));
227 * </code>
228 *
229 * @param string $cache Name of the cache.
230 * @param string $key Item key.
231 * @param array|string $item
232 *
233 * @return mixed
234 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
235 public function putItem($cache, $key, $item)
236 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
237 $cache = self::encodeCache($cache);
238 $key = self::encodeKey($key);
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
239 $itm = new IronCache_Item($item);
240 $req = $itm->asArray();
241 $url = "projects/{$this->project_id}/caches/$cache/items/$key";
79fbde3 @thousandsofthem First version
thousandsofthem authored
242
243 $this->setJsonHeaders();
244 $res = $this->apiCall(self::PUT, $url, $req);
245 return self::json_decode($res);
246 }
247
248 /**
249 * Get item from cache by key
250 *
251 * @param string $cache Cache name
252 * @param string $key Cache key
253 * @return mixed|null single item or null
254 * @throws Http_Exception
255 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
256 public function getItem($cache, $key)
257 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
258 $cache = self::encodeCache($cache);
259 $key = self::encodeKey($key);
260 $url = "projects/{$this->project_id}/caches/$cache/items/$key";
261
262 $this->setJsonHeaders();
263 try {
264 $res = $this->apiCall(self::GET, $url);
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
265 } catch (Http_Exception $e) {
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
266 if ($e->getCode() == Http_Exception::NOT_FOUND) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
267 return null;
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
268 } else {
79fbde3 @thousandsofthem First version
thousandsofthem authored
269 throw $e;
270 }
271 }
272 return self::json_decode($res);
273 }
274
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
275 public function deleteItem($cache, $key)
276 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
277 $cache = self::encodeCache($cache);
278 $key = self::encodeKey($key);
279 $url = "projects/{$this->project_id}/caches/$cache/items/$key";
280
281 $this->setJsonHeaders();
282 return self::json_decode($this->apiCall(self::DELETE, $url));
283 }
284
285 /**
286 * Atomically increments the value for key by amount.
287 * Can be used for both increment and decrement by passing a negative value.
288 * The value must exist and must be an integer.
289 * The number is treated as an unsigned 64-bit integer.
290 * The usual overflow rules apply when adding, but subtracting from 0 always yields 0.
291 *
292 * @param string $cache
293 * @param string $key
294 * @param int $amount Change by this value
295 * @return mixed|void
296 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
297 public function incrementItem($cache, $key, $amount = 1)
298 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
299 $cache = self::encodeCache($cache);
300 $key = self::encodeKey($key);
301 $url = "projects/{$this->project_id}/caches/$cache/items/$key/increment";
302 $params = array(
303 'amount' => $amount
304 );
305 $this->setJsonHeaders();
306 return self::json_decode($this->apiCall(self::POST, $url, $params));
307 }
308
309
310 /**
311 * Shortcut for getItem($cache, $key)
312 * Please set $cache name before use by setCacheName() method
313 *
314 * @param string $key
315 * @return mixed|null
316 * @throws InvalidArgumentException
317 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
318 public function get($key)
319 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
320 return $this->getItem($this->cache_name, $key);
321 }
322
323 /**
fb10436 @janpieper corrected method name (postItem --> putItem)
janpieper authored
324 * Shortcut for putItem($cache, $key, $item)
79fbde3 @thousandsofthem First version
thousandsofthem authored
325 * Please set $cache name before use by setCacheName() method
326 *
327 * @param string $key
328 * @param array|string $item
329 * @return mixed
330 * @throws InvalidArgumentException
331 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
332 public function put($key, $item)
333 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
334 return $this->putItem($this->cache_name, $key, $item);
335 }
336
337 /**
338 * Shortcut for deleteItem($cache, $key)
339 * Please set $cache name before use by setCacheName() method
340 *
341 * @param string $key
342 * @return mixed|void
343 * @throws InvalidArgumentException
344 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
345 public function delete($key)
346 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
347 return $this->deleteItem($this->cache_name, $key);
348 }
349
350 /**
351 * Shortcut for incrementItem($cache, $key, $amount)
352 * Please set $cache name before use by setCacheName() method
353 *
354 * @param string $key
355 * @param int $amount
356 * @return mixed|void
357 * @throws InvalidArgumentException
358 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
359 public function increment($key, $amount = 1)
360 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
361 return $this->incrementItem($this->cache_name, $key, $amount);
362 }
363
12e0f2c @thousandsofthem added clear() method. rebuild .phar package. bump version
thousandsofthem authored
364 /**
365 * Clear a Cache
366 * Delete all items in a cache. This cannot be undone.
367 *
368 * @param string|null $cache Cache name or null
369 * @return mixed
370 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
371 public function clear($cache = null)
372 {
373 if ($cache === null) {
12e0f2c @thousandsofthem added clear() method. rebuild .phar package. bump version
thousandsofthem authored
374 $cache = $this->cache_name;
375 }
376 $cache = self::encodeCache($cache);
377 $url = "projects/{$this->project_id}/caches/$cache/clear";
378 $params = array();
379 $this->setJsonHeaders();
380 return self::json_decode($this->apiCall(self::POST, $url, $params));
381 }
382
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
383
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
384 public function session_open($savePath, $sessionName)
385 {
7f86e3d @GrahamCampbell Coding style tweaks
GrahamCampbell authored
386 $this->setCacheName($sessionName);
387 return true;
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
388 }
389
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
390 public function session_close()
391 {
7f86e3d @GrahamCampbell Coding style tweaks
GrahamCampbell authored
392 return true;
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
393 }
394
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
395 public function session_read($id)
396 {
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
397 $item = $this->get($id);
398 if ($item !== null) {
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
399 return $item->value;
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
400 } else {
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
401 return null;
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
402 }
403 }
404
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
405 public function session_write($id, $data)
406 {
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
407 $this->put($id, array(
408 "value" => $data,
409 "expires_in" => $this->session_expire_time
410 ));
411 return true;
412 }
413
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
414 public function session_destroy($id)
415 {
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
416 try {
417 $this->delete($id);
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
418 } catch (Exception $e) {
419 # ignore any exceptions
420 }
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
421 return true;
422 }
423
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
424 public function session_gc($maxlifetime)
425 {
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
426 # auto-expire by default, no need for gc
427 return true;
428 }
429
430 /**
431 * Set IronCache as session store handler
432 *
433 * @param null|integer $session_expire_time Expire time in seconds
434 */
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
435 public function set_as_session_store($session_expire_time = null)
436 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
437 if ($session_expire_time != null) {
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
438 $this->session_expire_time = $session_expire_time;
439 }
440 session_set_save_handler(
7f86e3d @GrahamCampbell Coding style tweaks
GrahamCampbell authored
441 array($this, 'session_open'),
442 array($this, 'session_close'),
443 array($this, 'session_read'),
444 array($this, 'session_write'),
445 array($this, 'session_destroy'),
446 array($this, 'session_gc')
f0ba687 @thousandsofthem IronCache as session store
thousandsofthem authored
447 );
448 }
449
450
79fbde3 @thousandsofthem First version
thousandsofthem authored
451 /* PRIVATE FUNCTIONS */
452
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
453 protected static function encodeCache($cache)
454 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
455 if (empty($cache)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
456 throw new InvalidArgumentException('Please set $cache variable');
457 }
458 return rawurlencode($cache);
459 }
460
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
461 protected static function encodeKey($key)
462 {
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
463 if (empty($key)) {
79fbde3 @thousandsofthem First version
thousandsofthem authored
464 throw new InvalidArgumentException('Please set $key variable');
465 }
466 return rawurlencode($key);
467 }
468
469
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
470 protected function setJsonHeaders()
471 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
472 $this->setCommonHeaders();
473 }
474
d473e0d @GrahamCampbell Coding style fixes
GrahamCampbell authored
475 protected function setPostHeaders()
476 {
79fbde3 @thousandsofthem First version
thousandsofthem authored
477 $this->setCommonHeaders();
478 $this->headers['Content-Type'] ='multipart/form-data';
479 }
3f29392 @GrahamCampbell Syntax fixes
GrahamCampbell authored
480 }
Something went wrong with that request. Please try again.