From c8dfc329ae3aec59fd6e02f42e8f0aca1a30b30b Mon Sep 17 00:00:00 2001 From: Hector Mendoza Jacobo Date: Wed, 8 May 2024 20:14:36 +0000 Subject: [PATCH] Add a CacheChain prototype --- src/Cache/CacheChain.php | 153 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 src/Cache/CacheChain.php diff --git a/src/Cache/CacheChain.php b/src/Cache/CacheChain.php new file mode 100644 index 000000000..3fb7f4de0 --- /dev/null +++ b/src/Cache/CacheChain.php @@ -0,0 +1,153 @@ +addPool($pool); + } + } + + public function getItem(string $key): CacheItemInterface + { + foreach($this->pools as $pool) { + $item = $pool->getItem($key); + if ($item->isHit()) { + return $item; + } + } + + return new TypedItem($$key); + } + + public function getItems(array $keys = []): iterable + { + $result = []; + + foreach($keys as $key) { + $result[$key] = $this->getItem($key); + } + + return $result; + } + + public function hasItem(string $key): bool + { + foreach($this->pools as $pool) { + if($pool->hasItem($key)) { + return true; + } + } + + return false; + } + + public function clear(): bool + { + $result = true; + + foreach($this->pools as $pool) { + if(!$pool->clear()) { + $result = false; + } + } + + return $result; + } + + public function deleteItem(string $key): bool + { + $result = true; + + foreach($this->pools as $pool) { + if(!$pool->deleteItem($key)) { + $result = false; + } + } + + return $result; + } + + public function deleteItems(array $keys): bool + { + $result = true; + + foreach($keys as $key) { + if(!$this->deleteItem($key)) { + $result = false; + } + } + + return $result; + } + + public function save(CacheItemInterface $item): bool + { + $result = true; + + foreach($this->pools as $pool) { + if(!$pool->save($item)) { + $result = false; + } + } + + return $result; + } + + public function saveDeferred(CacheItemInterface $item): bool + { + $result = true; + + foreach($this->pools as $pool) { + if(!$pool->saveDeferred($item)) { + $result = false; + } + } + + return $result; + } + + public function commit(): bool + { + $result = true; + + foreach($this->pools as $pool) { + if(!$pool->commit()) { + $result = false; + } + } + + return $result; + } + + private function addPool(CacheItemPoolInterface $pool) + { + array_push($this->pools, $pool); + } + } \ No newline at end of file