AbstractAdapterTrait.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Log\LoggerAwareTrait;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. /**
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. *
  18. * @internal
  19. */
  20. trait AbstractAdapterTrait
  21. {
  22. use LoggerAwareTrait;
  23. /**
  24. * @var \Closure needs to be set by class, signature is function(string <key>, mixed <value>, bool <isHit>)
  25. */
  26. private $createCacheItem;
  27. /**
  28. * @var \Closure needs to be set by class, signature is function(array <deferred>, string <namespace>, array <&expiredIds>)
  29. */
  30. private $mergeByLifetime;
  31. private $namespace;
  32. private $namespaceVersion = '';
  33. private $versioningIsEnabled = false;
  34. private $deferred = [];
  35. private $ids = [];
  36. /**
  37. * @var int|null The maximum length to enforce for identifiers or null when no limit applies
  38. */
  39. protected $maxIdLength;
  40. /**
  41. * Fetches several cache items.
  42. *
  43. * @param array $ids The cache identifiers to fetch
  44. *
  45. * @return array|\Traversable The corresponding values found in the cache
  46. */
  47. abstract protected function doFetch(array $ids);
  48. /**
  49. * Confirms if the cache contains specified cache item.
  50. *
  51. * @param string $id The identifier for which to check existence
  52. *
  53. * @return bool True if item exists in the cache, false otherwise
  54. */
  55. abstract protected function doHave(string $id);
  56. /**
  57. * Deletes all items in the pool.
  58. *
  59. * @param string $namespace The prefix used for all identifiers managed by this pool
  60. *
  61. * @return bool True if the pool was successfully cleared, false otherwise
  62. */
  63. abstract protected function doClear(string $namespace);
  64. /**
  65. * Removes multiple items from the pool.
  66. *
  67. * @param array $ids An array of identifiers that should be removed from the pool
  68. *
  69. * @return bool True if the items were successfully removed, false otherwise
  70. */
  71. abstract protected function doDelete(array $ids);
  72. /**
  73. * Persists several cache items immediately.
  74. *
  75. * @param array $values The values to cache, indexed by their cache identifier
  76. * @param int $lifetime The lifetime of the cached values, 0 for persisting until manual cleaning
  77. *
  78. * @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not
  79. */
  80. abstract protected function doSave(array $values, int $lifetime);
  81. /**
  82. * {@inheritdoc}
  83. *
  84. * @return bool
  85. */
  86. public function hasItem($key)
  87. {
  88. $id = $this->getId($key);
  89. if (isset($this->deferred[$key])) {
  90. $this->commit();
  91. }
  92. try {
  93. return $this->doHave($id);
  94. } catch (\Exception $e) {
  95. CacheItem::log($this->logger, 'Failed to check if key "{key}" is cached: '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  96. return false;
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. *
  102. * @return bool
  103. */
  104. public function clear(string $prefix = '')
  105. {
  106. $this->deferred = [];
  107. if ($cleared = $this->versioningIsEnabled) {
  108. if ('' === $namespaceVersionToClear = $this->namespaceVersion) {
  109. foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) {
  110. $namespaceVersionToClear = $v;
  111. }
  112. }
  113. $namespaceToClear = $this->namespace.$namespaceVersionToClear;
  114. $namespaceVersion = strtr(substr_replace(base64_encode(pack('V', mt_rand())), static::NS_SEPARATOR, 5), '/', '_');
  115. try {
  116. $cleared = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0);
  117. } catch (\Exception $e) {
  118. $cleared = false;
  119. }
  120. if ($cleared = true === $cleared || [] === $cleared) {
  121. $this->namespaceVersion = $namespaceVersion;
  122. $this->ids = [];
  123. }
  124. } else {
  125. $namespaceToClear = $this->namespace.$prefix;
  126. }
  127. try {
  128. return $this->doClear($namespaceToClear) || $cleared;
  129. } catch (\Exception $e) {
  130. CacheItem::log($this->logger, 'Failed to clear the cache: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  131. return false;
  132. }
  133. }
  134. /**
  135. * {@inheritdoc}
  136. *
  137. * @return bool
  138. */
  139. public function deleteItem($key)
  140. {
  141. return $this->deleteItems([$key]);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. *
  146. * @return bool
  147. */
  148. public function deleteItems(array $keys)
  149. {
  150. $ids = [];
  151. foreach ($keys as $key) {
  152. $ids[$key] = $this->getId($key);
  153. unset($this->deferred[$key]);
  154. }
  155. try {
  156. if ($this->doDelete($ids)) {
  157. return true;
  158. }
  159. } catch (\Exception $e) {
  160. }
  161. $ok = true;
  162. // When bulk-delete failed, retry each item individually
  163. foreach ($ids as $key => $id) {
  164. try {
  165. $e = null;
  166. if ($this->doDelete([$id])) {
  167. continue;
  168. }
  169. } catch (\Exception $e) {
  170. }
  171. $message = 'Failed to delete key "{key}"'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
  172. CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  173. $ok = false;
  174. }
  175. return $ok;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function getItem($key)
  181. {
  182. if ($this->deferred) {
  183. $this->commit();
  184. }
  185. $id = $this->getId($key);
  186. $f = $this->createCacheItem;
  187. $isHit = false;
  188. $value = null;
  189. try {
  190. foreach ($this->doFetch([$id]) as $value) {
  191. $isHit = true;
  192. }
  193. return $f($key, $value, $isHit);
  194. } catch (\Exception $e) {
  195. CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  196. }
  197. return $f($key, null, false);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getItems(array $keys = [])
  203. {
  204. if ($this->deferred) {
  205. $this->commit();
  206. }
  207. $ids = [];
  208. foreach ($keys as $key) {
  209. $ids[] = $this->getId($key);
  210. }
  211. try {
  212. $items = $this->doFetch($ids);
  213. } catch (\Exception $e) {
  214. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  215. $items = [];
  216. }
  217. $ids = array_combine($ids, $keys);
  218. return $this->generateItems($items, $ids);
  219. }
  220. /**
  221. * {@inheritdoc}
  222. *
  223. * @return bool
  224. */
  225. public function save(CacheItemInterface $item)
  226. {
  227. if (!$item instanceof CacheItem) {
  228. return false;
  229. }
  230. $this->deferred[$item->getKey()] = $item;
  231. return $this->commit();
  232. }
  233. /**
  234. * {@inheritdoc}
  235. *
  236. * @return bool
  237. */
  238. public function saveDeferred(CacheItemInterface $item)
  239. {
  240. if (!$item instanceof CacheItem) {
  241. return false;
  242. }
  243. $this->deferred[$item->getKey()] = $item;
  244. return true;
  245. }
  246. /**
  247. * Enables/disables versioning of items.
  248. *
  249. * When versioning is enabled, clearing the cache is atomic and doesn't require listing existing keys to proceed,
  250. * but old keys may need garbage collection and extra round-trips to the back-end are required.
  251. *
  252. * Calling this method also clears the memoized namespace version and thus forces a resynchonization of it.
  253. *
  254. * @param bool $enable
  255. *
  256. * @return bool the previous state of versioning
  257. */
  258. public function enableVersioning($enable = true)
  259. {
  260. $wasEnabled = $this->versioningIsEnabled;
  261. $this->versioningIsEnabled = (bool) $enable;
  262. $this->namespaceVersion = '';
  263. $this->ids = [];
  264. return $wasEnabled;
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function reset()
  270. {
  271. if ($this->deferred) {
  272. $this->commit();
  273. }
  274. $this->namespaceVersion = '';
  275. $this->ids = [];
  276. }
  277. public function __sleep()
  278. {
  279. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  280. }
  281. public function __wakeup()
  282. {
  283. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  284. }
  285. public function __destruct()
  286. {
  287. if ($this->deferred) {
  288. $this->commit();
  289. }
  290. }
  291. private function generateItems(iterable $items, array &$keys): iterable
  292. {
  293. $f = $this->createCacheItem;
  294. try {
  295. foreach ($items as $id => $value) {
  296. if (!isset($keys[$id])) {
  297. throw new InvalidArgumentException(sprintf('Could not match value id "%s" to keys "%s".', $id, implode('", "', $keys)));
  298. }
  299. $key = $keys[$id];
  300. unset($keys[$id]);
  301. yield $key => $f($key, $value, true);
  302. }
  303. } catch (\Exception $e) {
  304. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  305. }
  306. foreach ($keys as $key) {
  307. yield $key => $f($key, null, false);
  308. }
  309. }
  310. private function getId($key)
  311. {
  312. if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
  313. $this->ids = [];
  314. $this->namespaceVersion = '1'.static::NS_SEPARATOR;
  315. try {
  316. foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) {
  317. $this->namespaceVersion = $v;
  318. }
  319. if ('1'.static::NS_SEPARATOR === $this->namespaceVersion) {
  320. $this->namespaceVersion = strtr(substr_replace(base64_encode(pack('V', time())), static::NS_SEPARATOR, 5), '/', '_');
  321. $this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0);
  322. }
  323. } catch (\Exception $e) {
  324. }
  325. }
  326. if (\is_string($key) && isset($this->ids[$key])) {
  327. return $this->namespace.$this->namespaceVersion.$this->ids[$key];
  328. }
  329. CacheItem::validateKey($key);
  330. $this->ids[$key] = $key;
  331. if (null === $this->maxIdLength) {
  332. return $this->namespace.$this->namespaceVersion.$key;
  333. }
  334. if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) {
  335. // Use MD5 to favor speed over security, which is not an issue here
  336. $this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 2));
  337. $id = $this->namespace.$this->namespaceVersion.$id;
  338. }
  339. return $id;
  340. }
  341. /**
  342. * @internal
  343. */
  344. public static function handleUnserializeCallback($class)
  345. {
  346. throw new \DomainException('Class not found: '.$class);
  347. }
  348. }