o2system /
cache
| 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\Cache\Abstracts; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Cache\Item; |
||||
| 19 | use Psr\Cache\CacheItemInterface; |
||||
|
0 ignored issues
–
show
|
|||||
| 20 | use Psr\Cache\CacheItemPoolInterface; |
||||
|
0 ignored issues
–
show
The type
Psr\Cache\CacheItemPoolInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 21 | use Psr\SimpleCache\CacheInterface; |
||||
|
0 ignored issues
–
show
The type
Psr\SimpleCache\CacheInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 22 | use O2System\Spl\Exceptions\Logic\InvalidArgumentException; |
||||
| 23 | use O2System\Spl\Iterators\ArrayIterator; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * Class AbstractItemPool |
||||
| 27 | * |
||||
| 28 | * @package O2System\Cache\Abstracts |
||||
| 29 | */ |
||||
| 30 | abstract class AbstractItemPool implements |
||||
| 31 | CacheItemPoolInterface, |
||||
| 32 | CacheInterface |
||||
| 33 | {
|
||||
| 34 | /** |
||||
| 35 | * AbstractItemPool::$storage |
||||
| 36 | * |
||||
| 37 | * Deferred Items Storage |
||||
| 38 | * |
||||
| 39 | * @var array |
||||
| 40 | */ |
||||
| 41 | private $deferred = []; |
||||
| 42 | |||||
| 43 | // ------------------------------------------------------------------------ |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Delete an item from the cache by its unique key. |
||||
| 47 | * |
||||
| 48 | * @param string $key The unique cache key of the item to delete. |
||||
| 49 | * |
||||
| 50 | * @return bool True if the item was successfully removed. False if there was an error. |
||||
| 51 | * |
||||
| 52 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 53 | * MUST be thrown if the $key string is not a legal value. |
||||
| 54 | */ |
||||
| 55 | public function delete($key) |
||||
| 56 | {
|
||||
| 57 | return (bool)$this->deleteItem($key); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | // ------------------------------------------------------------------------ |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * CacheItemPoolInterface::saveDeferred |
||||
| 64 | * |
||||
| 65 | * Sets a cache item to be persisted later. |
||||
| 66 | * |
||||
| 67 | * @param CacheItemInterface $item |
||||
| 68 | * The cache item to save. |
||||
| 69 | * |
||||
| 70 | * @return bool |
||||
| 71 | * False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||||
| 72 | */ |
||||
| 73 | public function saveDeferred(CacheItemInterface $item) |
||||
| 74 | {
|
||||
| 75 | $key = spl_object_id($item); |
||||
| 76 | |||||
| 77 | if ( ! array_key_exists($key, $this->deferred)) {
|
||||
| 78 | $this->deferred[] = $item; |
||||
| 79 | |||||
| 80 | return true; |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | return false; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | // ------------------------------------------------------------------------ |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * CacheItemPoolInterface::commit |
||||
| 90 | * |
||||
| 91 | * Persists any deferred cache items. |
||||
| 92 | * |
||||
| 93 | * @return bool |
||||
| 94 | * True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||||
| 95 | */ |
||||
| 96 | public function commit() |
||||
| 97 | {
|
||||
| 98 | $items = $this->deferred; |
||||
| 99 | |||||
| 100 | foreach ($items as $key => $item) {
|
||||
| 101 | if ($this->save($item) === true) {
|
||||
| 102 | unset($items[ $key ]); |
||||
| 103 | } |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | if (count($items) == 0) {
|
||||
| 107 | $this->deferred = []; |
||||
| 108 | |||||
| 109 | return true; |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | return false; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | // ------------------------------------------------------------------------ |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Obtains multiple cache items by their unique keys. |
||||
| 119 | * |
||||
| 120 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||||
| 121 | * @param mixed $default Default value to return for keys that do not exist. |
||||
| 122 | * |
||||
| 123 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as |
||||
| 124 | * value. |
||||
| 125 | * |
||||
| 126 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 127 | */ |
||||
| 128 | public function getMultiple($keys, $default = null) |
||||
| 129 | {
|
||||
| 130 | $result = new ArrayIterator(); |
||||
| 131 | |||||
| 132 | foreach ($keys as $key) {
|
||||
| 133 | if ($this->has($key)) {
|
||||
| 134 | $result[ $key ] = $this->get($key, $default); |
||||
| 135 | } |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | return $result; |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | // ------------------------------------------------------------------------ |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * Determines whether an item is present in the cache. |
||||
| 145 | * |
||||
| 146 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||||
| 147 | * and not to be used within your live applications operations for get/set, as this method |
||||
| 148 | * is subject to a race condition where your has() will return true and immediately after, |
||||
| 149 | * another script can remove it making the state of your app out of date. |
||||
| 150 | * |
||||
| 151 | * @param string $key The cache item key. |
||||
| 152 | * |
||||
| 153 | * @return bool |
||||
| 154 | * |
||||
| 155 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 156 | * MUST be thrown if the $key string is not a legal value. |
||||
| 157 | */ |
||||
| 158 | public function has($key) |
||||
| 159 | {
|
||||
| 160 | return (bool)$this->hasItem($key); |
||||
| 161 | } |
||||
| 162 | |||||
| 163 | // ------------------------------------------------------------------------ |
||||
| 164 | |||||
| 165 | /** |
||||
| 166 | * Fetches a value from the cache. |
||||
| 167 | * |
||||
| 168 | * @param string $key The unique key of this item in the cache. |
||||
| 169 | * @param mixed $default Default value to return if the key does not exist. |
||||
| 170 | * |
||||
| 171 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||||
| 172 | * |
||||
| 173 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 174 | * MUST be thrown if the $key string is not a legal value. |
||||
| 175 | */ |
||||
| 176 | public function get($key, $default = null) |
||||
| 177 | {
|
||||
| 178 | if ($this->hasItem($key)) {
|
||||
| 179 | return $this->getItem($key); |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | return $default; |
||||
| 183 | } |
||||
| 184 | |||||
| 185 | // ------------------------------------------------------------------------ |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||||
| 189 | * |
||||
| 190 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||||
| 191 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||||
| 192 | * the driver supports TTL then the library may set a default value |
||||
| 193 | * for it or let the driver take care of that. |
||||
| 194 | * |
||||
| 195 | * @return bool True on success and false on failure. |
||||
| 196 | */ |
||||
| 197 | public function setMultiple($values, $ttl = null) |
||||
| 198 | {
|
||||
| 199 | $result = []; |
||||
| 200 | |||||
| 201 | foreach ($values as $key => $value) {
|
||||
| 202 | if ($this->set($key, $value, $ttl)) {
|
||||
| 203 | $result[ $key ] = true; |
||||
| 204 | } |
||||
| 205 | } |
||||
| 206 | |||||
| 207 | return (bool)count($result) == count($values); |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | // ------------------------------------------------------------------------ |
||||
| 211 | |||||
| 212 | /** |
||||
| 213 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||||
| 214 | * |
||||
| 215 | * @param string $key The key of the item to store. |
||||
| 216 | * @param mixed $value The value of the item to store, must be serializable. |
||||
| 217 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||||
| 218 | * the driver supports TTL then the library may set a default value |
||||
| 219 | * for it or let the driver take care of that. |
||||
| 220 | * |
||||
| 221 | * @return bool True on success and false on failure. |
||||
| 222 | */ |
||||
| 223 | public function set($key, $value, $ttl = null) |
||||
| 224 | {
|
||||
| 225 | return $this->save(new Item($key, $value, $ttl)); |
||||
| 226 | } |
||||
| 227 | |||||
| 228 | // ------------------------------------------------------------------------ |
||||
| 229 | |||||
| 230 | /** |
||||
| 231 | * Deletes multiple cache items in a single operation. |
||||
| 232 | * |
||||
| 233 | * @param iterable $keys A list of string-based keys to be deleted. |
||||
| 234 | * |
||||
| 235 | * @return bool True if the items were successfully removed. False if there was an error. |
||||
| 236 | * |
||||
| 237 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 238 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||||
| 239 | */ |
||||
| 240 | public function deleteMultiple($keys) |
||||
| 241 | {
|
||||
| 242 | 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\Cache\Abstracts...ItemPool::deleteItems().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 243 | } |
||||
| 244 | |||||
| 245 | // ------------------------------------------------------------------------ |
||||
| 246 | |||||
| 247 | /** |
||||
| 248 | * CacheItemPoolInterface::deleteItems |
||||
| 249 | * |
||||
| 250 | * Removes multiple items from the pool. |
||||
| 251 | * |
||||
| 252 | * @param string[] $keys |
||||
| 253 | * An array of keys that should be removed from the pool. |
||||
| 254 | * |
||||
| 255 | * @return bool |
||||
| 256 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||||
| 257 | * @throws \Psr\Cache\InvalidArgumentException |
||||
| 258 | */ |
||||
| 259 | public function deleteItems(array $keys) |
||||
| 260 | {
|
||||
| 261 | if ( ! is_array($keys)) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 262 | throw new InvalidArgumentException('CACHE_E_INVALID_ARGUMENT_ARRAY');
|
||||
| 263 | } |
||||
| 264 | |||||
| 265 | foreach ($keys as $key) {
|
||||
| 266 | if ($this->deleteItem($key) === false) {
|
||||
| 267 | return false; |
||||
| 268 | break; |
||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||
| 269 | } |
||||
| 270 | } |
||||
| 271 | |||||
| 272 | return true; |
||||
| 273 | } |
||||
| 274 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths