o2system /
framework
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Framework\Services; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Cache\Abstracts\AbstractItemPool; |
||||
| 19 | use O2System\Cache\Adapters; |
||||
| 20 | use O2System\Cache\Item; |
||||
| 21 | use Psr\Cache\CacheItemInterface; |
||||
| 22 | use Psr\Cache\CacheItemPoolInterface; |
||||
| 23 | use Psr\SimpleCache\CacheInterface; |
||||
| 24 | use O2System\Spl\Iterators\ArrayIterator; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * Class Cache |
||||
| 28 | * @package O2System\Framework\Services |
||||
| 29 | */ |
||||
| 30 | class Cache extends Adapters implements CacheItemPoolInterface, CacheInterface |
||||
| 31 | { |
||||
| 32 | /** |
||||
| 33 | * Cache::$poolOffset |
||||
| 34 | * |
||||
| 35 | * @var string |
||||
| 36 | */ |
||||
| 37 | private $poolOffset = 'default'; |
||||
| 38 | |||||
| 39 | // ------------------------------------------------------------------------ |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Cache::setItemPool |
||||
| 43 | * |
||||
| 44 | * @param string $poolOffset |
||||
| 45 | * |
||||
| 46 | * @return static |
||||
| 47 | */ |
||||
| 48 | public function setItemPool($poolOffset) |
||||
| 49 | { |
||||
| 50 | if ($this->exists($poolOffset)) { |
||||
| 51 | $this->poolOffset = $poolOffset; |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | return $this; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | // ------------------------------------------------------------------------ |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Cache::getItems |
||||
| 61 | * |
||||
| 62 | * Returns a traversable set of cache items. |
||||
| 63 | * |
||||
| 64 | * @param string[] $keys |
||||
| 65 | * An indexed array of keys of items to retrieve. |
||||
| 66 | * |
||||
| 67 | * @throws InvalidArgumentException |
||||
| 68 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||||
| 69 | * MUST be thrown. |
||||
| 70 | * |
||||
| 71 | * @return array|\Traversable |
||||
| 72 | * A traversable collection of Cache Items keyed by the cache keys of |
||||
| 73 | * each item. A Cache item will be returned for each key, even if that |
||||
| 74 | * key is not found. However, if no keys are specified then an empty |
||||
| 75 | * traversable MUST be returned instead. |
||||
| 76 | */ |
||||
| 77 | public function getItems(array $keys = []) |
||||
| 78 | { |
||||
| 79 | if (false !== ($cacheItem = $this->callPoolOffset('getItems', [$keys]))) { |
||||
| 80 | return $cacheItem; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 81 | } |
||||
| 82 | |||||
| 83 | return []; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | // ------------------------------------------------------------------------ |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Cache::callPoolOffset |
||||
| 90 | * |
||||
| 91 | * @param string $method |
||||
| 92 | * @param array $args |
||||
| 93 | * |
||||
| 94 | * @return bool|mixed |
||||
| 95 | */ |
||||
| 96 | public function callPoolOffset($method, array $args = []) |
||||
| 97 | { |
||||
| 98 | $poolOffset = $this->poolOffset; |
||||
| 99 | |||||
| 100 | if ( ! $this->exists($poolOffset)) { |
||||
| 101 | $poolOffset = 'default'; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | $itemPool = $this->getItemPool($poolOffset); |
||||
| 105 | |||||
| 106 | if ($itemPool instanceof AbstractItemPool) { |
||||
| 107 | if (method_exists($itemPool, $method)) { |
||||
| 108 | return call_user_func_array([&$itemPool, $method], $args); |
||||
| 109 | } |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | return false; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | // ------------------------------------------------------------------------ |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Cache::clear |
||||
| 119 | * |
||||
| 120 | * Deletes all items in the pool. |
||||
| 121 | * |
||||
| 122 | * @return bool |
||||
| 123 | * True if the pool was successfully cleared. False if there was an error. |
||||
| 124 | */ |
||||
| 125 | public function clear() |
||||
| 126 | { |
||||
| 127 | return $this->callPoolOffset('clear'); |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | // ------------------------------------------------------------------------ |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Cache::saveDeferred |
||||
| 134 | * |
||||
| 135 | * Sets a cache item to be persisted later. |
||||
| 136 | * |
||||
| 137 | * @param CacheItemInterface $item |
||||
| 138 | * The cache item to save. |
||||
| 139 | * |
||||
| 140 | * @return bool |
||||
| 141 | * False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||||
| 142 | */ |
||||
| 143 | public function saveDeferred(CacheItemInterface $item) |
||||
| 144 | { |
||||
| 145 | return $this->callPoolOffset('saveDeferred', [$item]); |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | // ------------------------------------------------------------------------ |
||||
| 149 | |||||
| 150 | /** |
||||
| 151 | * Cache::commit |
||||
| 152 | * |
||||
| 153 | * Persists any deferred cache items. |
||||
| 154 | * |
||||
| 155 | * @return bool |
||||
| 156 | * True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||||
| 157 | */ |
||||
| 158 | public function commit() |
||||
| 159 | { |
||||
| 160 | return $this->callPoolOffset('commit'); |
||||
| 161 | } |
||||
| 162 | |||||
| 163 | // ------------------------------------------------------------------------ |
||||
| 164 | |||||
| 165 | /** |
||||
| 166 | * Cache::delete |
||||
| 167 | * |
||||
| 168 | * Delete an item from the cache by its unique key. |
||||
| 169 | * |
||||
| 170 | * @param string $key The unique cache key of the item to delete. |
||||
| 171 | * |
||||
| 172 | * @return bool True if the item was successfully removed. False if there was an error. |
||||
| 173 | * |
||||
| 174 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 175 | * MUST be thrown if the $key string is not a legal value. |
||||
| 176 | */ |
||||
| 177 | public function delete($key) |
||||
| 178 | { |
||||
| 179 | return (bool)$this->deleteItem($key); |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | // ------------------------------------------------------------------------ |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * Cache::deleteItem |
||||
| 186 | * |
||||
| 187 | * Removes the item from the pool. |
||||
| 188 | * |
||||
| 189 | * @param string $key |
||||
| 190 | * The key to delete. |
||||
| 191 | * |
||||
| 192 | * @throws InvalidArgumentException |
||||
| 193 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||||
| 194 | * MUST be thrown. |
||||
| 195 | * |
||||
| 196 | * @return bool |
||||
| 197 | * True if the item was successfully removed. False if there was an error. |
||||
| 198 | */ |
||||
| 199 | public function deleteItem($key) |
||||
| 200 | { |
||||
| 201 | return $this->callPoolOffset('deleteItem', [$key]); |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | // ------------------------------------------------------------------------ |
||||
| 205 | |||||
| 206 | /** |
||||
| 207 | * Cache::getMultiple |
||||
| 208 | * |
||||
| 209 | * Obtains multiple cache items by their unique keys. |
||||
| 210 | * |
||||
| 211 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||||
| 212 | * @param mixed $default Default value to return for keys that do not exist. |
||||
| 213 | * |
||||
| 214 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as |
||||
| 215 | * value. |
||||
| 216 | * |
||||
| 217 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 218 | * MUST be thrown if $keys is neither an array nor a Traversable, |
||||
| 219 | * or if any of the $keys are not a legal value. |
||||
| 220 | */ |
||||
| 221 | public function getMultiple($keys, $default = null) |
||||
| 222 | { |
||||
| 223 | $result = new ArrayIterator(); |
||||
| 224 | |||||
| 225 | foreach ($keys as $key) { |
||||
| 226 | if ($this->exists($key)) { |
||||
| 227 | $result[ $key ] = $this->get($key, $default); |
||||
| 228 | } |
||||
| 229 | } |
||||
| 230 | |||||
| 231 | return $result; |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | // ------------------------------------------------------------------------ |
||||
| 235 | |||||
| 236 | /** |
||||
| 237 | * Cache::get |
||||
| 238 | * |
||||
| 239 | * Fetches a value from the cache. |
||||
| 240 | * |
||||
| 241 | * @param string $key The unique key of this item in the cache. |
||||
| 242 | * @param mixed $default Default value to return if the key does not exist. |
||||
| 243 | * |
||||
| 244 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||||
| 245 | * |
||||
| 246 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 247 | * @throws \Exception |
||||
| 248 | * MUST be thrown if the $key string is not a legal value. |
||||
| 249 | */ |
||||
| 250 | public function get($key, $default = null) |
||||
| 251 | { |
||||
| 252 | if ($this->hasItem($key)) { |
||||
| 253 | $item = $this->getItem($key); |
||||
| 254 | |||||
| 255 | return $item->get(); |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | return $default; |
||||
| 259 | } |
||||
| 260 | |||||
| 261 | // ------------------------------------------------------------------------ |
||||
| 262 | |||||
| 263 | /** |
||||
| 264 | * Cache::hasItem |
||||
| 265 | * |
||||
| 266 | * Confirms if the cache contains specified cache item. |
||||
| 267 | * |
||||
| 268 | * Note: This method MAY avoid retrieving the cached value for performance reasons. |
||||
| 269 | * This could result in a race condition with CacheItemInterface::get(). To avoid |
||||
| 270 | * such situation use CacheItemInterface::isHit() instead. |
||||
| 271 | * |
||||
| 272 | * @param string $key |
||||
| 273 | * The key for which to check existence. |
||||
| 274 | * |
||||
| 275 | * @throws InvalidArgumentException |
||||
| 276 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||||
| 277 | * MUST be thrown. |
||||
| 278 | * |
||||
| 279 | * @return bool |
||||
| 280 | * True if item exists in the cache, false otherwise. |
||||
| 281 | */ |
||||
| 282 | public function hasItem($key) |
||||
| 283 | { |
||||
| 284 | return $this->callPoolOffset('hasItem', [$key]); |
||||
| 285 | } |
||||
| 286 | |||||
| 287 | // ------------------------------------------------------------------------ |
||||
| 288 | |||||
| 289 | /** |
||||
| 290 | * Cache::getItem |
||||
| 291 | * |
||||
| 292 | * Returns a Cache Item representing the specified key. |
||||
| 293 | * |
||||
| 294 | * This method must always return a CacheItemInterface object, even in case of |
||||
| 295 | * a cache miss. It MUST NOT return null. |
||||
| 296 | * |
||||
| 297 | * @param string $key |
||||
| 298 | * The key for which to return the corresponding Cache Item. |
||||
| 299 | * |
||||
| 300 | * @throws InvalidArgumentException |
||||
| 301 | * @throws \Exception |
||||
| 302 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||||
| 303 | * MUST be thrown. |
||||
| 304 | * |
||||
| 305 | * @return CacheItemInterface |
||||
| 306 | * The corresponding Cache Item. |
||||
| 307 | */ |
||||
| 308 | public function getItem($key) |
||||
| 309 | { |
||||
| 310 | if (false !== ($cacheItem = $this->callPoolOffset('getItem', [$key]))) { |
||||
| 311 | return $cacheItem; |
||||
|
0 ignored issues
–
show
|
|||||
| 312 | } |
||||
| 313 | |||||
| 314 | return new Item(null, null); |
||||
| 315 | } |
||||
| 316 | |||||
| 317 | // ------------------------------------------------------------------------ |
||||
| 318 | |||||
| 319 | /** |
||||
| 320 | * Cache::setMultiple |
||||
| 321 | * |
||||
| 322 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||||
| 323 | * |
||||
| 324 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||||
| 325 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||||
| 326 | * the driver supports TTL then the library may set a default value |
||||
| 327 | * for it or let the driver take care of that. |
||||
| 328 | * |
||||
| 329 | * @return bool True on success and false on failure. |
||||
| 330 | * |
||||
| 331 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 332 | * MUST be thrown if $values is neither an array nor a Traversable, |
||||
| 333 | * or if any of the $values are not a legal value. |
||||
| 334 | */ |
||||
| 335 | public function setMultiple($values, $ttl = null) |
||||
| 336 | { |
||||
| 337 | $result = []; |
||||
| 338 | |||||
| 339 | foreach ($values as $key => $value) { |
||||
| 340 | if ($this->set($key, $value, $ttl)) { |
||||
| 341 | $result[ $key ] = true; |
||||
| 342 | } |
||||
| 343 | } |
||||
| 344 | |||||
| 345 | return (bool)count($result) == count($values); |
||||
| 346 | } |
||||
| 347 | |||||
| 348 | // ------------------------------------------------------------------------ |
||||
| 349 | |||||
| 350 | /** |
||||
| 351 | * Cache::set |
||||
| 352 | * |
||||
| 353 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||||
| 354 | * |
||||
| 355 | * @param string $key The key of the item to store. |
||||
| 356 | * @param mixed $value The value of the item to store, must be serializable. |
||||
| 357 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||||
| 358 | * the driver supports TTL then the library may set a default value |
||||
| 359 | * for it or let the driver take care of that. |
||||
| 360 | * |
||||
| 361 | * @return bool True on success and false on failure. |
||||
| 362 | * |
||||
| 363 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 364 | * @throws \Exception |
||||
| 365 | * MUST be thrown if the $key string is not a legal value. |
||||
| 366 | */ |
||||
| 367 | public function set($key, $value, $ttl = null) |
||||
| 368 | { |
||||
| 369 | return $this->save(new Item($key, $value, $ttl)); |
||||
| 370 | } |
||||
| 371 | |||||
| 372 | // ------------------------------------------------------------------------ |
||||
| 373 | |||||
| 374 | /** |
||||
| 375 | * Cache::save |
||||
| 376 | * |
||||
| 377 | * Persists a cache item immediately. |
||||
| 378 | * |
||||
| 379 | * @param CacheItemInterface $item |
||||
| 380 | * The cache item to save. |
||||
| 381 | * |
||||
| 382 | * @return bool |
||||
| 383 | * True if the item was successfully persisted. False if there was an error. |
||||
| 384 | */ |
||||
| 385 | public function save(CacheItemInterface $item) |
||||
| 386 | { |
||||
| 387 | return $this->callPoolOffset('save', [$item]); |
||||
| 388 | } |
||||
| 389 | |||||
| 390 | // ------------------------------------------------------------------------ |
||||
| 391 | |||||
| 392 | /** |
||||
| 393 | * Cache::deleteMultiple |
||||
| 394 | * |
||||
| 395 | * Deletes multiple cache items in a single operation. |
||||
| 396 | * |
||||
| 397 | * @param iterable $keys A list of string-based keys to be deleted. |
||||
| 398 | * |
||||
| 399 | * @return bool True if the items were successfully removed. False if there was an error. |
||||
| 400 | * |
||||
| 401 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 402 | * MUST be thrown if $keys is neither an array nor a Traversable, |
||||
| 403 | * or if any of the $keys are not a legal value. |
||||
| 404 | */ |
||||
| 405 | public function deleteMultiple($keys) |
||||
| 406 | { |
||||
| 407 | return (bool)$this->deleteItems($keys); |
||||
|
0 ignored issues
–
show
$keys of type iterable is incompatible with the type array expected by parameter $keys of O2System\Framework\Services\Cache::deleteItems().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 408 | } |
||||
| 409 | |||||
| 410 | // ------------------------------------------------------------------------ |
||||
| 411 | |||||
| 412 | /** |
||||
| 413 | * Adapters::deleteItems |
||||
| 414 | * |
||||
| 415 | * Removes multiple items from the pool. |
||||
| 416 | * |
||||
| 417 | * @param string[] $keys |
||||
| 418 | * An array of keys that should be removed from the pool. |
||||
| 419 | * |
||||
| 420 | * @throws InvalidArgumentException |
||||
| 421 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||||
| 422 | * MUST be thrown. |
||||
| 423 | * |
||||
| 424 | * @return bool |
||||
| 425 | * True if the items were successfully removed. False if there was an error. |
||||
| 426 | */ |
||||
| 427 | public function deleteItems(array $keys = []) |
||||
| 428 | { |
||||
| 429 | return $this->callPoolOffset('deleteItems', [$keys]); |
||||
| 430 | } |
||||
| 431 | |||||
| 432 | // ------------------------------------------------------------------------ |
||||
| 433 | |||||
| 434 | /** |
||||
| 435 | * Cache::has |
||||
| 436 | * |
||||
| 437 | * Determines whether an item is present in the cache. |
||||
| 438 | * |
||||
| 439 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||||
| 440 | * and not to be used within your live applications operations for get/set, as this method |
||||
| 441 | * is subject to a race condition where your has() will return true and immediately after, |
||||
| 442 | * another script can remove it making the state of your app out of date. |
||||
| 443 | * |
||||
| 444 | * @param string $key The cache item key. |
||||
| 445 | * |
||||
| 446 | * @return bool |
||||
| 447 | * |
||||
| 448 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||||
| 449 | * MUST be thrown if the $key string is not a legal value. |
||||
| 450 | */ |
||||
| 451 | public function has($key) |
||||
| 452 | { |
||||
| 453 | return $this->hasItem($key); |
||||
| 454 | } |
||||
| 455 | } |