From 4cafdf13dec20442ff051f768377742a45af4258 Mon Sep 17 00:00:00 2001 From: Georges Date: Fri, 2 Feb 2024 22:56:40 +0100 Subject: [PATCH] GC optimization --- .../Core/Pool/CacheItemPoolTrait.php | 20 +++++++++++++------ .../Core/Pool/ExtendedCacheItemPoolTrait.php | 6 +----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php index 2c9f29af..ffe06b68 100644 --- a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php @@ -31,13 +31,11 @@ use Phpfastcache\Event\Event\CacheItemPoolEventGetItems; use Phpfastcache\Event\Event\CacheSaveDeferredItemItemPoolEvent; use Phpfastcache\Event\Event\CacheItemPoolEventSaveItem; -use Phpfastcache\Event\Events; use Phpfastcache\Event\EventManagerInterface; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheCoreException; use Phpfastcache\Exceptions\PhpfastcacheDriverException; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; -use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException; use Phpfastcache\Exceptions\PhpfastcacheIOException; use Phpfastcache\Exceptions\PhpfastcacheLogicException; use Phpfastcache\Exceptions\PhpfastcacheUnsupportedMethodException; @@ -113,6 +111,7 @@ public function setItem(CacheItemInterface $item): static */ public function getItems(array $keys = []): iterable { + $gcStatus = gc_enabled(); $items = []; $config = $this->getConfig(); @@ -139,6 +138,9 @@ public function getItems(array $keys = []): iterable * If there's still keys to fetch, let's choose the right method (if supported). */ if (\count($keys) > 1) { + if ($gcStatus) { + gc_disable(); + } $items = \array_merge( \array_combine($keys, \array_map(fn($key) => new (self::getItemClass())($this, $key, $this->eventManager), $keys)), $items @@ -176,6 +178,9 @@ public function getItems(array $keys = []): iterable } $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); } + if ($gcStatus) { + gc_enable(); + } } } else { $index = \array_key_first($keys); @@ -204,7 +209,6 @@ public function getItems(array $keys = []): iterable * @throws PhpfastcacheCoreException * @throws PhpfastcacheInvalidArgumentException * @throws PhpfastcacheLogicException - * @throws PhpfastcacheDriverException * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.GotoStatement) @@ -534,12 +538,16 @@ protected function deregisterItem(string $itemKey): static } /** - * @param string[] $itemKeys + * @param ?string[] $itemKeys * @internal This method de-register multiple items from $this->itemInstances */ - protected function deregisterItems(array $itemKeys): static + protected function deregisterItems(?array $itemKeys): static { - $this->itemInstances = \array_diff_key($this->itemInstances, \array_flip($itemKeys)); + if ($itemKeys !== null) { + $this->itemInstances = \array_diff_key($this->itemInstances, \array_flip($itemKeys)); + } else { + $this->itemInstances = []; + } if (\gc_enabled()) { \gc_collect_cycles(); diff --git a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php index b871cc0b..05398650 100644 --- a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php @@ -76,11 +76,7 @@ public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THRO public function detachAllItems(): static { - foreach ($this->itemInstances as $item) { - $this->detachItem($item); - } - - return $this; + return $this->deregisterItems(null); } public function detachItem(CacheItemInterface $item): static