diff --git a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php index 8316487a3..648508751 100644 --- a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php @@ -50,7 +50,7 @@ public function getItemsByTag($tagName): array if (\is_string($tagName)) { $driverResponse = $this->getItem($this->getTagKey($tagName)); if ($driverResponse->isHit()) { - $items = (array)$driverResponse->get(); + $items = (array) $driverResponse->get(); /** * getItems() may provides expired item(s) @@ -80,13 +80,13 @@ public function getItemsByTags(array $tagNames): array $items = []; foreach (\array_unique($tagNames) as $tagName) { if (\is_string($tagName)) { - $items = \array_merge($items, $this->getItemsByTag($tagName)); + $items[] = $this->getItemsByTag($tagName); } else { throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); } } - return $items; + return \array_merge([], ...$items); } @@ -98,7 +98,7 @@ public function getItemsByTagsAll(array $tagNames): array $items = $this->getItemsByTags($tagNames); foreach ($items as $key => $item) { - if (\array_diff($tagNames, $item->getTags())) { + if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { unset($items[$key]); } } diff --git a/tests/issues/Github-713.test.php b/tests/issues/Github-713.test.php new file mode 100644 index 000000000..18b874187 --- /dev/null +++ b/tests/issues/Github-713.test.php @@ -0,0 +1,41 @@ + https://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ + +use Phpfastcache\CacheManager; +use Phpfastcache\Drivers\Files\Config as FilesConfig; +use Phpfastcache\Helper\TestHelper; + +chdir(__DIR__); +require_once __DIR__ . '/../../vendor/autoload.php'; +$testHelper = new TestHelper('Github issue #713 - Api Method "deleteItemsByTagsAll()" removes unrelated items'); +$cacheInstance = CacheManager::getInstance('Files', new FilesConfig(['securityKey' => 'test-713'])); +$cacheInstance->clear(); + +$item1 = $cacheInstance->getItem('item1'); +$item2 = $cacheInstance->getItem('item2'); + +$item1->addTags(['shared', 'custom1']); +$item1->set(1337); + +$item2->addTags(['shared']); +$item2->set(1337); + +$cacheInstance->saveMultiple($item1, $item2); +$cacheInstance->detachAllItems(); +unset($item1, $item2); +$cacheInstance->deleteItemsByTagsAll(['shared']); + +$item1 = $cacheInstance->getItem('item1'); +$item2 = $cacheInstance->getItem('item2'); + +if ($item1->isHit()) { + $testHelper->printPassText('Item #1 is still in cache as expected.'); +} else { + $testHelper->printFailText('Item #1 is no longer in cache and so has been unexpectedly removed.'); +} + +$testHelper->terminateTest(); \ No newline at end of file