From 050154874085ce5466dd0a77743184fa8b7c3126 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Tue, 3 Dec 2019 23:51:04 +0100 Subject: [PATCH 1/2] Fixed #713 --- .../Core/Pool/ExtendedCacheItemPoolTrait.php | 8 ++-- tests/issues/Github-713.test.php | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/issues/Github-713.test.php 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..f353a43f4 --- /dev/null +++ b/tests/issues/Github-713.test.php @@ -0,0 +1,44 @@ + https://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ + +use Phpfastcache\CacheManager; +use Phpfastcache\Drivers\Memcached\Config; +use Phpfastcache\Drivers\Files\Config as FilesConfig; +use Phpfastcache\Exceptions\PhpfastcacheDriverException; +use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; +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 From b4d21b1402ae47c28aaceabbe5ae0ac093c231d3 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Tue, 3 Dec 2019 23:52:49 +0100 Subject: [PATCH 2/2] Cleaned up test code --- tests/issues/Github-713.test.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/issues/Github-713.test.php b/tests/issues/Github-713.test.php index f353a43f4..18b874187 100644 --- a/tests/issues/Github-713.test.php +++ b/tests/issues/Github-713.test.php @@ -6,10 +6,7 @@ */ use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Memcached\Config; use Phpfastcache\Drivers\Files\Config as FilesConfig; -use Phpfastcache\Exceptions\PhpfastcacheDriverException; -use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; use Phpfastcache\Helper\TestHelper; chdir(__DIR__); @@ -35,9 +32,9 @@ $item1 = $cacheInstance->getItem('item1'); $item2 = $cacheInstance->getItem('item2'); -if($item1->isHit()){ +if ($item1->isHit()) { $testHelper->printPassText('Item #1 is still in cache as expected.'); -}else{ +} else { $testHelper->printFailText('Item #1 is no longer in cache and so has been unexpectedly removed.'); }