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