|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | +namespaceSymfony\Component\Cache\Adapter; |
| 13 | + |
| 14 | +usePsr\Cache\CacheItemInterface; |
| 15 | +usePsr\Cache\CacheItemPoolInterface; |
| 16 | +useSymfony\Component\Cache\CacheItem; |
| 17 | +useSymfony\Component\Cache\Exception\InvalidArgumentException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Nicolas Grekas <p@tchwork.com> |
| 21 | + */ |
| 22 | +abstractclass AbstractAdapterimplements CacheItemPoolInterface |
| 23 | +{ |
| 24 | +private$namespace; |
| 25 | +private$deferred =array(); |
| 26 | +private$createCacheItem; |
| 27 | +private$mergeByLifetime; |
| 28 | + |
| 29 | +protectedfunction__construct($namespace ='',$defaultLifetime =0) |
| 30 | + { |
| 31 | +$this->namespace =$namespace; |
| 32 | +$this->createCacheItem = \Closure::bind( |
| 33 | +function ($key,$value,$isHit)use ($defaultLifetime) { |
| 34 | +$item =newCacheItem(); |
| 35 | +$item->key =$key; |
| 36 | +$item->value =$value; |
| 37 | +$item->isHit =$isHit; |
| 38 | +$item->defaultLifetime =$defaultLifetime; |
| 39 | + |
| 40 | +return$item; |
| 41 | + }, |
| 42 | +$this, |
| 43 | + CacheItem::class |
| 44 | + ); |
| 45 | +$this->mergeByLifetime = \Closure::bind( |
| 46 | +function ($deferred,$namespace) { |
| 47 | +$byLifetime =array(); |
| 48 | + |
| 49 | +foreach ($deferredas$key =>$item) { |
| 50 | +if (0 <=$item->lifetime) { |
| 51 | +$byLifetime[(int)$item->lifetime][$namespace.$key] =$item->value; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +return$byLifetime; |
| 56 | + }, |
| 57 | +$this, |
| 58 | + CacheItem::class |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | +/** |
| 63 | + * Fetches several cache items. |
| 64 | + * |
| 65 | + * @param array $ids The cache identifiers to fetch. |
| 66 | + * |
| 67 | + * @return array The corresponding values found in the cache. |
| 68 | + */ |
| 69 | +abstractprotectedfunctiondoFetch(array$ids); |
| 70 | + |
| 71 | +/** |
| 72 | + * Confirms if the cache contains specified cache item. |
| 73 | + * |
| 74 | + * @param string $id The identifier for which to check existence. |
| 75 | + * |
| 76 | + * @return bool True if item exists in the cache, false otherwise. |
| 77 | + */ |
| 78 | +abstractprotectedfunctiondoHave($id); |
| 79 | + |
| 80 | +/** |
| 81 | + * Deletes all items in the pool. |
| 82 | + * |
| 83 | + * @return bool True if the pool was successfully cleared, false otherwise. |
| 84 | + */ |
| 85 | +abstractprotectedfunctiondoClear(); |
| 86 | + |
| 87 | +/** |
| 88 | + * Removes multiple items from the pool. |
| 89 | + * |
| 90 | + * @param array $ids An array of identifiers that should be removed from the pool. |
| 91 | + * |
| 92 | + * @return bool True if the items were successfully removed, false otherwise. |
| 93 | + */ |
| 94 | +abstractprotectedfunctiondoDelete(array$ids); |
| 95 | + |
| 96 | +/** |
| 97 | + * Persists several cache items immediately. |
| 98 | + * |
| 99 | + * @param array $values The values to cache, indexed by their cache identifier. |
| 100 | + * @param int $lifetime The lifetime of the cached values, 0 for persisting until manual cleaning. |
| 101 | + * |
| 102 | + * @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not. |
| 103 | + */ |
| 104 | +abstractprotectedfunctiondoSave(array$values,$lifetime); |
| 105 | + |
| 106 | +/** |
| 107 | + * {@inheritdoc} |
| 108 | + */ |
| 109 | +publicfunctiongetItem($key) |
| 110 | + { |
| 111 | +$id =$this->getId($key); |
| 112 | + |
| 113 | +if ($this->deferred) { |
| 114 | +$this->commit(); |
| 115 | + } |
| 116 | +if (isset($this->deferred[$key])) { |
| 117 | +return$this->deferred[$key]; |
| 118 | + } |
| 119 | + |
| 120 | +$f =$this->createCacheItem; |
| 121 | +$isHit =false; |
| 122 | +$value =null; |
| 123 | + |
| 124 | +foreach ($this->doFetch(array($id))as$value) { |
| 125 | +$isHit =true; |
| 126 | + } |
| 127 | + |
| 128 | +return$f($key,$value,$isHit); |
| 129 | + } |
| 130 | + |
| 131 | +/** |
| 132 | + * {@inheritdoc} |
| 133 | + */ |
| 134 | +publicfunctiongetItems(array$keys =array()) |
| 135 | + { |
| 136 | +if ($this->deferred) { |
| 137 | +$this->commit(); |
| 138 | + } |
| 139 | +$f =$this->createCacheItem; |
| 140 | +$ids =array(); |
| 141 | +$items =array(); |
| 142 | + |
| 143 | +foreach ($keysas$key) { |
| 144 | +$id =$this->getId($key); |
| 145 | + |
| 146 | +if (isset($this->deferred[$key])) { |
| 147 | +$items[$key] =$this->deferred[$key]; |
| 148 | + }else { |
| 149 | +$ids[$key] =$id; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +$values =$this->doFetch($ids); |
| 154 | + |
| 155 | +foreach ($idsas$key =>$id) { |
| 156 | +$isHit =isset($values[$id]); |
| 157 | +$items[$key] =$f($key,$isHit ?$values[$id] :null,$isHit); |
| 158 | + } |
| 159 | + |
| 160 | +return$items; |
| 161 | + } |
| 162 | + |
| 163 | +/** |
| 164 | + * {@inheritdoc} |
| 165 | + */ |
| 166 | +publicfunctionhasItem($key) |
| 167 | + { |
| 168 | +if ($this->deferred) { |
| 169 | +$this->commit(); |
| 170 | + } |
| 171 | + |
| 172 | +return$this->doHave($this->getId($key)); |
| 173 | + } |
| 174 | + |
| 175 | +/** |
| 176 | + * {@inheritdoc} |
| 177 | + */ |
| 178 | +publicfunctionclear() |
| 179 | + { |
| 180 | +$this->deferred =array(); |
| 181 | + |
| 182 | +return$this->doClear(); |
| 183 | + } |
| 184 | + |
| 185 | +/** |
| 186 | + * {@inheritdoc} |
| 187 | + */ |
| 188 | +publicfunctiondeleteItem($key) |
| 189 | + { |
| 190 | +return$this->deleteItems(array($key)); |
| 191 | + } |
| 192 | + |
| 193 | +/** |
| 194 | + * {@inheritdoc} |
| 195 | + */ |
| 196 | +publicfunctiondeleteItems(array$keys) |
| 197 | + { |
| 198 | +$ids =array(); |
| 199 | + |
| 200 | +foreach ($keysas$key) { |
| 201 | +$ids[] =$this->getId($key); |
| 202 | + unset($this->deferred[$key]); |
| 203 | + } |
| 204 | + |
| 205 | +return$this->doDelete($ids); |
| 206 | + } |
| 207 | + |
| 208 | +/** |
| 209 | + * {@inheritdoc} |
| 210 | + */ |
| 211 | +publicfunctionsave(CacheItemInterface$item) |
| 212 | + { |
| 213 | +if (!$iteminstanceof CacheItem) { |
| 214 | +returnfalse; |
| 215 | + } |
| 216 | +$key =$item->getKey(); |
| 217 | +$this->deferred[$key] =$item; |
| 218 | +$this->commit(); |
| 219 | + |
| 220 | +return !isset($this->deferred[$key]); |
| 221 | + } |
| 222 | + |
| 223 | +/** |
| 224 | + * {@inheritdoc} |
| 225 | + */ |
| 226 | +publicfunctionsaveDeferred(CacheItemInterface$item) |
| 227 | + { |
| 228 | +if (!$iteminstanceof CacheItem) { |
| 229 | +returnfalse; |
| 230 | + } |
| 231 | +try { |
| 232 | +$item =clone$item; |
| 233 | + }catch (\Error$e) { |
| 234 | + }catch (\Exception$e) { |
| 235 | + } |
| 236 | +if (isset($e)) { |
| 237 | + @trigger_error($e->__toString()); |
| 238 | + |
| 239 | +returnfalse; |
| 240 | + } |
| 241 | +$this->deferred[$item->getKey()] =$item; |
| 242 | + |
| 243 | +returntrue; |
| 244 | + } |
| 245 | + |
| 246 | +/** |
| 247 | + * {@inheritdoc} |
| 248 | + */ |
| 249 | +publicfunctioncommit() |
| 250 | + { |
| 251 | +$f =$this->mergeByLifetime; |
| 252 | +$ko =array(); |
| 253 | +$namespaceLen =strlen($this->namespace); |
| 254 | + |
| 255 | +foreach ($f($this->deferred,$this->namespace)as$lifetime =>$values) { |
| 256 | +if (true ===$ok =$this->doSave($values,$lifetime)) { |
| 257 | +continue; |
| 258 | + } |
| 259 | +if (false ===$ok) { |
| 260 | +$ok =array_keys($values); |
| 261 | + } |
| 262 | +foreach ($okas$failedId) { |
| 263 | +$key =substr($failedId,$namespaceLen); |
| 264 | +$ko[$key] =$this->deferred[$key]; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | +return !$this->deferred =$ko; |
| 269 | + } |
| 270 | + |
| 271 | +publicfunction__destruct() |
| 272 | + { |
| 273 | +if ($this->deferred) { |
| 274 | +$this->commit(); |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | +privatefunctiongetId($key) |
| 279 | + { |
| 280 | +if (!is_string($key)) { |
| 281 | +thrownewInvalidArgumentException(sprintf('Cache key must be string, "%s" given',is_object($key) ?get_class($key) :gettype($key))); |
| 282 | + } |
| 283 | +if (!isset($key[0])) { |
| 284 | +thrownewInvalidArgumentException('Cache key length must be greater than zero'); |
| 285 | + } |
| 286 | +if (isset($key[strcspn($key,'{}()/\@:')])) { |
| 287 | +thrownewInvalidArgumentException('Cache key contains reserved characters {}()/\@:'); |
| 288 | + } |
| 289 | + |
| 290 | +return$this->namespace.$key; |
| 291 | + } |
| 292 | +} |