Нет описания

PSRMetadataCache.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace MailPoet\Doctrine;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Psr\Cache\CacheItemInterface;
  5. use MailPoetVendor\Psr\Cache\CacheItemPoolInterface;
  6. class PSRMetadataCache implements CacheItemPoolInterface {
  7. /** @var MetadataCache */
  8. private $metadataCache;
  9. public function __construct(string $dir, bool $isReadOnly) {
  10. $this->metadataCache = new MetadataCache($dir, $isReadOnly);
  11. }
  12. /**
  13. * @inheritDoc
  14. */
  15. public function getItem($key): CacheItemInterface {
  16. if (!$this->hasItem($key)) {
  17. return new PSRCacheItem($key, false);
  18. }
  19. $item = new PSRCacheItem($key, true);
  20. $item->set($this->metadataCache->fetch($key));
  21. return $item;
  22. }
  23. /**
  24. * @inheritDoc
  25. */
  26. public function getItems(array $keys = []) {
  27. if (empty($keys)) {
  28. return [];
  29. }
  30. $foundItems = [];
  31. // no internal array function supports this sort of mapping: needs to be iterative
  32. // this filters and combines keys in one pass
  33. foreach ($keys as $key) {
  34. if (!is_string($key)) {
  35. throw new PSRCacheInvalidArgumentException('Invalid key');
  36. }
  37. $foundItems[$key] = $this->getItem($key);
  38. }
  39. return $foundItems;
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. public function hasItem($key): bool {
  45. return $this->metadataCache->contains($key);
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. public function clear(): bool {
  51. return $this->metadataCache->flushAll();
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. public function deleteItem($key): bool {
  57. return $this->metadataCache->delete($key);
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function deleteItems(array $keys): bool {
  63. if (empty($keys)) {
  64. return true;
  65. }
  66. foreach ($keys as $key) {
  67. $this->deleteItem($key);
  68. }
  69. return true;
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function save(CacheItemInterface $item) {
  75. try {
  76. return $this->metadataCache->save($item->getKey(), $item->get());
  77. } catch (\RuntimeException $e) {
  78. throw new PSRCacheInvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  79. }
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. public function saveDeferred(CacheItemInterface $item) {
  85. return $this->save($item);
  86. }
  87. /**
  88. * @inheritDoc
  89. */
  90. public function commit(): bool {
  91. return true;
  92. }
  93. }