| @@ 21-164 (lines=144) @@ | ||
| 18 | * |
|
| 19 | * @since 1.0 |
|
| 20 | */ |
|
| 21 | class Apc extends Cache |
|
| 22 | { |
|
| 23 | /** |
|
| 24 | * Constructor. |
|
| 25 | * |
|
| 26 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
|
| 27 | * |
|
| 28 | * @since 1.0 |
|
| 29 | * @throws \RuntimeException |
|
| 30 | */ |
|
| 31 | public function __construct($options = array()) |
|
| 32 | { |
|
| 33 | if (!extension_loaded('apc') || !is_callable('apc_fetch')) |
|
| 34 | { |
|
| 35 | throw new UnsupportedFormatException('APC not supported.'); |
|
| 36 | } |
|
| 37 | ||
| 38 | parent::__construct($options); |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * This will wipe out the entire cache's keys |
|
| 43 | * |
|
| 44 | * @return boolean The result of the clear operation. |
|
| 45 | * |
|
| 46 | * @since 1.0 |
|
| 47 | */ |
|
| 48 | public function clear() |
|
| 49 | { |
|
| 50 | return apc_clear_cache('user'); |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Method to get a storage entry value from a key. |
|
| 55 | * |
|
| 56 | * @param string $key The storage entry identifier. |
|
| 57 | * |
|
| 58 | * @return CacheItemInterface |
|
| 59 | * |
|
| 60 | * @since 1.0 |
|
| 61 | * @throws \RuntimeException |
|
| 62 | */ |
|
| 63 | public function getItem($key) |
|
| 64 | { |
|
| 65 | $success = false; |
|
| 66 | $value = apc_fetch($key, $success); |
|
| 67 | $item = new Item($key); |
|
| 68 | ||
| 69 | if ($success) |
|
| 70 | { |
|
| 71 | $item->set($value); |
|
| 72 | } |
|
| 73 | ||
| 74 | return $item; |
|
| 75 | } |
|
| 76 | ||
| 77 | /** |
|
| 78 | * Obtain multiple CacheItems by their unique keys. |
|
| 79 | * |
|
| 80 | * @param array $keys A list of keys that can obtained in a single operation. |
|
| 81 | * |
|
| 82 | * @return array An associative array of CacheItem objects keyed on the cache key. |
|
| 83 | * |
|
| 84 | * @since 1.0 |
|
| 85 | */ |
|
| 86 | public function getItems(array $keys = array()) |
|
| 87 | { |
|
| 88 | $items = array(); |
|
| 89 | $success = false; |
|
| 90 | $values = apc_fetch($keys, $success); |
|
| 91 | ||
| 92 | if ($success && is_array($values)) |
|
| 93 | { |
|
| 94 | foreach ($keys as $key) |
|
| 95 | { |
|
| 96 | $items[$key] = new Item($key); |
|
| 97 | ||
| 98 | if (isset($values[$key])) |
|
| 99 | { |
|
| 100 | $items[$key]->set($values[$key]); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | } |
|
| 104 | ||
| 105 | return $items; |
|
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * Method to remove a storage entry for a key. |
|
| 110 | * |
|
| 111 | * @param string $key The storage entry identifier. |
|
| 112 | * |
|
| 113 | * @return boolean |
|
| 114 | * |
|
| 115 | * @since 1.0 |
|
| 116 | */ |
|
| 117 | public function deleteItem($key) |
|
| 118 | { |
|
| 119 | if ($this->hasItem($key)) |
|
| 120 | { |
|
| 121 | return apc_delete($key); |
|
| 122 | } |
|
| 123 | ||
| 124 | // If the item doesn't exist, no error |
|
| 125 | return true; |
|
| 126 | } |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Persists a cache item immediately. |
|
| 130 | * |
|
| 131 | * @param CacheItemInterface $item The cache item to save. |
|
| 132 | * |
|
| 133 | * @return static |
|
| 134 | * The invoked object. |
|
| 135 | */ |
|
| 136 | public function save(CacheItemInterface $item) |
|
| 137 | { |
|
| 138 | // If we are able to find out when the item expires - find out. Else bail. |
|
| 139 | if ($item instanceof HasExpirationDateInterface) |
|
| 140 | { |
|
| 141 | $ttl = $this->convertItemExpiryToSeconds($item); |
|
| 142 | } |
|
| 143 | else |
|
| 144 | { |
|
| 145 | $ttl = 0; |
|
| 146 | } |
|
| 147 | ||
| 148 | return apc_store($item->getKey(), $item->get(), $ttl); |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Method to determine whether a storage entry has been set for a key. |
|
| 153 | * |
|
| 154 | * @param string $key The storage entry identifier. |
|
| 155 | * |
|
| 156 | * @return boolean |
|
| 157 | * |
|
| 158 | * @since 1.0 |
|
| 159 | */ |
|
| 160 | public function hasItem($key) |
|
| 161 | { |
|
| 162 | return apc_exists($key); |
|
| 163 | } |
|
| 164 | } |
|
| 165 | ||
| @@ 21-164 (lines=144) @@ | ||
| 18 | * |
|
| 19 | * @since __DEPLOY_VERSION__ |
|
| 20 | */ |
|
| 21 | class Apcu extends Cache |
|
| 22 | { |
|
| 23 | /** |
|
| 24 | * Constructor. |
|
| 25 | * |
|
| 26 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
|
| 27 | * |
|
| 28 | * @since __DEPLOY_VERSION__ |
|
| 29 | * @throws \RuntimeException |
|
| 30 | */ |
|
| 31 | public function __construct($options = array()) |
|
| 32 | { |
|
| 33 | if (!extension_loaded('apc') || !is_callable('apcu_fetch')) |
|
| 34 | { |
|
| 35 | throw new UnsupportedFormatException('APC not supported.'); |
|
| 36 | } |
|
| 37 | ||
| 38 | parent::__construct($options); |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * This will wipe out the entire cache's keys |
|
| 43 | * |
|
| 44 | * @return boolean The result of the clear operation. |
|
| 45 | * |
|
| 46 | * @since __DEPLOY_VERSION__ |
|
| 47 | */ |
|
| 48 | public function clear() |
|
| 49 | { |
|
| 50 | return apcu_clear_cache('user'); |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Method to get a storage entry value from a key. |
|
| 55 | * |
|
| 56 | * @param string $key The storage entry identifier. |
|
| 57 | * |
|
| 58 | * @return CacheItemInterface |
|
| 59 | * |
|
| 60 | * @since __DEPLOY_VERSION__ |
|
| 61 | * @throws \RuntimeException |
|
| 62 | */ |
|
| 63 | public function getItem($key) |
|
| 64 | { |
|
| 65 | $success = false; |
|
| 66 | $value = apcu_fetch($key, $success); |
|
| 67 | $item = new Item($key); |
|
| 68 | ||
| 69 | if ($success) |
|
| 70 | { |
|
| 71 | $item->set($value); |
|
| 72 | } |
|
| 73 | ||
| 74 | return $item; |
|
| 75 | } |
|
| 76 | ||
| 77 | /** |
|
| 78 | * Obtain multiple CacheItems by their unique keys. |
|
| 79 | * |
|
| 80 | * @param array $keys A list of keys that can obtained in a single operation. |
|
| 81 | * |
|
| 82 | * @return array An associative array of CacheItem objects keyed on the cache key. |
|
| 83 | * |
|
| 84 | * @since __DEPLOY_VERSION__ |
|
| 85 | */ |
|
| 86 | public function getItems(array $keys = array()) |
|
| 87 | { |
|
| 88 | $items = array(); |
|
| 89 | $success = false; |
|
| 90 | $values = apcu_fetch($keys, $success); |
|
| 91 | ||
| 92 | if ($success && is_array($values)) |
|
| 93 | { |
|
| 94 | foreach ($keys as $key) |
|
| 95 | { |
|
| 96 | $items[$key] = new Item($key); |
|
| 97 | ||
| 98 | if (isset($values[$key])) |
|
| 99 | { |
|
| 100 | $items[$key]->set($values[$key]); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | } |
|
| 104 | ||
| 105 | return $items; |
|
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * Method to remove a storage entry for a key. |
|
| 110 | * |
|
| 111 | * @param string $key The storage entry identifier. |
|
| 112 | * |
|
| 113 | * @return boolean |
|
| 114 | * |
|
| 115 | * @since __DEPLOY_VERSION__ |
|
| 116 | */ |
|
| 117 | public function deleteItem($key) |
|
| 118 | { |
|
| 119 | if ($this->hasItem($key)) |
|
| 120 | { |
|
| 121 | return apcu_delete($key); |
|
| 122 | } |
|
| 123 | ||
| 124 | // If the item doesn't exist, no error |
|
| 125 | return true; |
|
| 126 | } |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Persists a cache item immediately. |
|
| 130 | * |
|
| 131 | * @param CacheItemInterface $item The cache item to save. |
|
| 132 | * |
|
| 133 | * @return static |
|
| 134 | * The invoked object. |
|
| 135 | */ |
|
| 136 | public function save(CacheItemInterface $item) |
|
| 137 | { |
|
| 138 | // If we are able to find out when the item expires - find out. Else bail. |
|
| 139 | if ($item instanceof HasExpirationDateInterface) |
|
| 140 | { |
|
| 141 | $ttl = $this->convertItemExpiryToSeconds($item); |
|
| 142 | } |
|
| 143 | else |
|
| 144 | { |
|
| 145 | $ttl = 0; |
|
| 146 | } |
|
| 147 | ||
| 148 | return apcu_store($item->getKey(), $item->get(), $ttl); |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Method to determine whether a storage entry has been set for a key. |
|
| 153 | * |
|
| 154 | * @param string $key The storage entry identifier. |
|
| 155 | * |
|
| 156 | * @return boolean |
|
| 157 | * |
|
| 158 | * @since __DEPLOY_VERSION__ |
|
| 159 | */ |
|
| 160 | public function hasItem($key) |
|
| 161 | { |
|
| 162 | return apcu_exists($key); |
|
| 163 | } |
|
| 164 | } |
|
| 165 | ||