|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Domino Cache Store |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2013 Domino Co. Ltd. |
| 6 | + * @license MIT |
| 7 | + * @package Domino_CacheStore |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Domino\CacheStore\Storage; |
| 11 | + |
| 12 | +/** |
| 13 | + * Domino Cache Store Storage for APC |
| 14 | + * |
| 15 | + * @package Domino_CacheStore |
| 16 | + * @subpackage Storage |
| 17 | + */ |
| 18 | +class Apc implements StorageInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Namespace separator |
| 22 | + */ |
| 23 | + const NAMESPACE_SEPARATOR = '\\'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Default expiration time (sec) |
| 27 | + * @var integer |
| 28 | + */ |
| 29 | + private $default_ttl = 360; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructor |
| 33 | + * @param array $option cache store storage option |
| 34 | + */ |
| 35 | + public function __construct($option = array()) |
| 36 | + { |
| 37 | + $this->default_ttl = $option['default_ttl']; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set item |
| 42 | + * @param string $namespace |
| 43 | + * @param string $key |
| 44 | + * @param mixed $value |
| 45 | + * @param integer $ttl expiration time (sec) |
| 46 | + */ |
| 47 | + public function set($namespace, $key, $value, $ttl = null) |
| 48 | + { |
| 49 | + $store_key = $this->generateStoreKey($namespace, $key); |
| 50 | + $expire = is_null($ttl) ? $this->default_ttl : $ttl; |
| 51 | + |
| 52 | + return apc_store($store_key, $value, $expire); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get item |
| 57 | + * @param string $namespace |
| 58 | + * @param string $key |
| 59 | + * @return mixed stored item |
| 60 | + */ |
| 61 | + public function get($namespace, $key) |
| 62 | + { |
| 63 | + $store_key = $this->generateStoreKey($namespace, $key); |
| 64 | + |
| 65 | + return apc_exists($store_key) ? apc_fetch($store_key) : null; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Clear item |
| 70 | + * @param string $namespace |
| 71 | + * @param string $key |
| 72 | + * @return boolean success or failure |
| 73 | + */ |
| 74 | + public function clear($namespace, $key) |
| 75 | + { |
| 76 | + $store_key = $this->generateStoreKey($namespace, $key); |
| 77 | + |
| 78 | + return apc_delete($store_key); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Clear by namespace |
| 83 | + * @param string $namespace |
| 84 | + * @return boolean success or failure |
| 85 | + */ |
| 86 | + public function clearByNamespace($namespace) |
| 87 | + { |
| 88 | + $pattern = '/^' . preg_quote($namespace . self::NAMESPACE_SEPARATOR) . '/'; |
| 89 | + |
| 90 | + return apc_delete(new \APCIterator('user', $pattern, APC_ITER_VALUE)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Clear all |
| 95 | + * @return boolean success or failure |
| 96 | + */ |
| 97 | + public function clearAll() |
| 98 | + { |
| 99 | + return apc_clear_cache("user"); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Generate internal store key |
| 104 | + * @param string $namespace |
| 105 | + * @param string $key |
| 106 | + * @return string internal store key |
| 107 | + */ |
| 108 | + private function generateStoreKey($namespace, $key) |
| 109 | + { |
| 110 | + return $namespace . self::NAMESPACE_SEPARATOR . $key; |
| 111 | + } |
| 112 | +} |
0 commit comments