|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Psr\Cache; |
| 13 | + |
| 14 | +use CodeIgniter\I18n\Time; |
| 15 | +use DateInterval; |
| 16 | +use Psr\SimpleCache\CacheInterface; |
| 17 | +use Traversable; |
| 18 | + |
| 19 | +final class SimpleCache implements CacheInterface |
| 20 | +{ |
| 21 | + use SupportTrait; |
| 22 | + |
| 23 | + /** |
| 24 | + * Fetches a value from the cache. |
| 25 | + * |
| 26 | + * @param string $key The unique key of this item in the cache. |
| 27 | + * @param mixed $default Default value to return if the key does not exist. |
| 28 | + * |
| 29 | + * @return mixed The value of the item from the cache, or $default in case of cache miss. |
| 30 | + * |
| 31 | + * @throws CacheArgumentException |
| 32 | + * MUST be thrown if the $key string is not a legal value. |
| 33 | + */ |
| 34 | + public function get($key, $default = null) |
| 35 | + { |
| 36 | + self::validateKey($key); |
| 37 | + |
| 38 | + $meta = $this->adapter->getMetaData($key); |
| 39 | + |
| 40 | + // If the adapter does not return an array or if the item is expired then it is a miss |
| 41 | + if (! is_array($meta) || (is_int($meta['expire']) && $meta['expire'] < time())) |
| 42 | + { |
| 43 | + return $default; |
| 44 | + } |
| 45 | + |
| 46 | + return $this->adapter->get($key); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
| 51 | + * |
| 52 | + * @param string $key The key of the item to store. |
| 53 | + * @param mixed $value The value of the item to store. Must be serializable. |
| 54 | + * @param null|integer|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
| 55 | + * the driver supports TTL then the library may set a default value |
| 56 | + * for it or let the driver take care of that. |
| 57 | + * |
| 58 | + * @return boolean True on success and false on failure. |
| 59 | + * |
| 60 | + * @throws CacheArgumentException |
| 61 | + * MUST be thrown if the $key string is not a legal value. |
| 62 | + */ |
| 63 | + public function set($key, $value, $ttl = null) |
| 64 | + { |
| 65 | + self::validateKey($key); |
| 66 | + |
| 67 | + // Get TTL as an integer (seconds) |
| 68 | + if (is_null($ttl)) |
| 69 | + { |
| 70 | + $ttl = 60; |
| 71 | + } |
| 72 | + elseif ($ttl instanceof DateInterval) |
| 73 | + { |
| 74 | + $ttl = $ttl->s; |
| 75 | + } |
| 76 | + elseif (! is_int($ttl)) |
| 77 | + { |
| 78 | + throw new CacheArgumentException('TTL value must be one of: null, integer, DateInterval.'); |
| 79 | + } |
| 80 | + |
| 81 | + // Do not save expired items |
| 82 | + if ($ttl <= 0) |
| 83 | + { |
| 84 | + $this->delete($key); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + return $this->adapter->save($key, $value, $ttl); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Delete an item from the cache by its unique key. |
| 93 | + * |
| 94 | + * @param string $key The unique cache key of the item to delete. |
| 95 | + * |
| 96 | + * @return boolean True if the item was successfully removed. False if there was an error. |
| 97 | + * |
| 98 | + * @throws CacheArgumentException |
| 99 | + * MUST be thrown if the $key string is not a legal value. |
| 100 | + */ |
| 101 | + public function delete($key) |
| 102 | + { |
| 103 | + self::validateKey($key); |
| 104 | + |
| 105 | + // Nonexistant keys return true |
| 106 | + if (! is_array($this->adapter->getMetaData($key))) |
| 107 | + { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + return $this->adapter->delete($key); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Wipes clean the entire cache's keys. |
| 116 | + * |
| 117 | + * @return boolean True on success and false on failure. |
| 118 | + */ |
| 119 | + public function clear() |
| 120 | + { |
| 121 | + return $this->adapter->clean(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Obtains multiple cache items by their unique keys. |
| 126 | + * |
| 127 | + * @param iterable $keys A list of keys that can obtained in a single operation. |
| 128 | + * @param mixed $default Default value to return for keys that do not exist. |
| 129 | + * |
| 130 | + * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
| 131 | + * |
| 132 | + * @throws CacheArgumentException |
| 133 | + * MUST be thrown if $keys is neither an array nor a Traversable, |
| 134 | + * or if any of the $keys are not a legal value. |
| 135 | + */ |
| 136 | + public function getMultiple($keys, $default = null) |
| 137 | + { |
| 138 | + if (! (is_array($keys) || $keys instanceof Traversable)) |
| 139 | + { |
| 140 | + throw new CacheArgumentException('getMultiple only accepts traversable input.'); |
| 141 | + } |
| 142 | + |
| 143 | + // CacheInterface has no spec for multiple item retrieval |
| 144 | + // so we have to power through them individually. |
| 145 | + $items = []; |
| 146 | + foreach ($keys as $key) |
| 147 | + { |
| 148 | + $items[$key] = $this->get($key, $default); |
| 149 | + } |
| 150 | + |
| 151 | + return $items; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Persists a set of key => value pairs in the cache, with an optional TTL. |
| 156 | + * |
| 157 | + * @param iterable $values A list of key => value pairs for a multiple-set operation. |
| 158 | + * @param null|integer|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
| 159 | + * the driver supports TTL then the library may set a default value |
| 160 | + * for it or let the driver take care of that. |
| 161 | + * |
| 162 | + * @return boolean True on success and false on failure. |
| 163 | + * |
| 164 | + * @throws CacheArgumentException |
| 165 | + * MUST be thrown if $values is neither an array nor a Traversable, |
| 166 | + * or if any of the $values are not a legal value. |
| 167 | + */ |
| 168 | + public function setMultiple($values, $ttl = null) |
| 169 | + { |
| 170 | + if (! (is_array($values) || $values instanceof Traversable)) |
| 171 | + { |
| 172 | + throw new CacheArgumentException('setMultiple only accepts traversable input.'); |
| 173 | + } |
| 174 | + |
| 175 | + // CacheInterface has no spec for multiple item storage |
| 176 | + // so we have to power through them individually. |
| 177 | + $return = true; |
| 178 | + foreach ($values as $key => $value) |
| 179 | + { |
| 180 | + if (is_int($key)) |
| 181 | + { |
| 182 | + $key = (string) $key; |
| 183 | + } |
| 184 | + $result = $this->set($key, $value, $ttl); |
| 185 | + $return = $result && $return; |
| 186 | + } |
| 187 | + |
| 188 | + return $return; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Deletes multiple cache items in a single operation. |
| 193 | + * |
| 194 | + * @param iterable $keys A list of string-based keys to be deleted. |
| 195 | + * |
| 196 | + * @return boolean True if the items were successfully removed. False if there was an error. |
| 197 | + * |
| 198 | + * @throws CacheArgumentException |
| 199 | + * MUST be thrown if $keys is neither an array nor a Traversable, |
| 200 | + * or if any of the $keys are not a legal value. |
| 201 | + */ |
| 202 | + public function deleteMultiple($keys) |
| 203 | + { |
| 204 | + if (! (is_array($keys) || $keys instanceof Traversable)) |
| 205 | + { |
| 206 | + throw new CacheArgumentException('deleteMultiple only accepts traversable input.'); |
| 207 | + } |
| 208 | + |
| 209 | + // CacheInterface has no spec for multiple item removal |
| 210 | + // so we have to power through them individually. |
| 211 | + $return = true; |
| 212 | + foreach ($keys as $key) |
| 213 | + { |
| 214 | + $result = $this->delete($key); |
| 215 | + $return = $result && $return; |
| 216 | + } |
| 217 | + |
| 218 | + return $return; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Determines whether an item is present in the cache. |
| 223 | + * |
| 224 | + * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
| 225 | + * and not to be used within your live applications operations for get/set, as this method |
| 226 | + * is subject to a race condition where your has() will return true and immediately after, |
| 227 | + * another script can remove it, making the state of your app out of date. |
| 228 | + * |
| 229 | + * @param string $key The cache item key. |
| 230 | + * |
| 231 | + * @return boolean |
| 232 | + * |
| 233 | + * @throws CacheArgumentException |
| 234 | + * MUST be thrown if the $key string is not a legal value. |
| 235 | + */ |
| 236 | + public function has($key) |
| 237 | + { |
| 238 | + self::validateKey($key); |
| 239 | + |
| 240 | + $meta = $this->adapter->getMetaData($key); |
| 241 | + |
| 242 | + // If the adapter does not return an array or if the item is expired then it is a miss |
| 243 | + if (! is_array($meta) || (is_int($meta['expire']) && $meta['expire'] < time())) |
| 244 | + { |
| 245 | + return false; |
| 246 | + } |
| 247 | + |
| 248 | + return true; |
| 249 | + } |
| 250 | +} |
0 commit comments