Skip to content

Commit

Permalink
GC optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Feb 2, 2024
1 parent d688495 commit 4cafdf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 14 additions & 6 deletions lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,6 +111,7 @@ public function setItem(CacheItemInterface $item): static
*/
public function getItems(array $keys = []): iterable
{
$gcStatus = gc_enabled();
$items = [];
$config = $this->getConfig();

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -204,7 +209,6 @@ public function getItems(array $keys = []): iterable
* @throws PhpfastcacheCoreException
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheDriverException
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.GotoStatement)
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4cafdf1

Please sign in to comment.