Skip to content

Fixed #713 #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}


Expand All @@ -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]);
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/issues/Github-713.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <[email protected]>
*/

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();