From 0cf083181033d23c08d3bcb4b23199da9526fdfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 3 Dec 2024 10:28:06 +0100 Subject: [PATCH] Create a cached wishlist checker --- src/Checker/CachedWishlistChecker.php | 29 +++++++++++++++++++++++ src/Resources/config/services/checker.xml | 5 ++++ 2 files changed, 34 insertions(+) create mode 100644 src/Checker/CachedWishlistChecker.php diff --git a/src/Checker/CachedWishlistChecker.php b/src/Checker/CachedWishlistChecker.php new file mode 100644 index 0000000..7737d1b --- /dev/null +++ b/src/Checker/CachedWishlistChecker.php @@ -0,0 +1,29 @@ + */ + private array $cache = []; + + public function __construct(private readonly WishlistCheckerInterface $decorated) + { + } + + public function onWishlist(ProductInterface|ProductVariantInterface $product): bool + { + $key = spl_object_hash($product); + + if (!array_key_exists($key, $this->cache)) { + $this->cache[$key] = $this->decorated->onWishlist($product); + } + + return $this->cache[$key]; + } +} diff --git a/src/Resources/config/services/checker.xml b/src/Resources/config/services/checker.xml index ec6d95a..8af5d25 100644 --- a/src/Resources/config/services/checker.xml +++ b/src/Resources/config/services/checker.xml @@ -8,5 +8,10 @@ + + + +