diff --git a/lib/Phpfastcache/CacheManager.php b/lib/Phpfastcache/CacheManager.php index 599c9a68e..056c60434 100644 --- a/lib/Phpfastcache/CacheManager.php +++ b/lib/Phpfastcache/CacheManager.php @@ -193,12 +193,8 @@ public static function getInstanceById(string $instanceId): ExtendedCacheItemPoo } /** - * This method is intended for internal - * use only and should not be used for - * any external development use the - * getInstances() method instead + * Return the list of instances * - * @internal * @return ExtendedCacheItemPoolInterface[] */ public static function getInstances(): array @@ -279,6 +275,25 @@ public static function clearInstances(): bool return !\count(self::$instances); } + /** + * @param ExtendedCacheItemPoolInterface $cachePoolInstance + * @return bool + * @since 7.0.4 + */ + public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolInstance): bool + { + $found = false; + self::$instances = \array_filter(\array_map(function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found){ + if(\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)){ + $found = true; + return null; + } + return $cachePool; + }, self::$instances)); + + return $found; + } + /** * @return string */ diff --git a/tests/CacheClearSingleInstance.test.php b/tests/CacheClearSingleInstance.test.php new file mode 100644 index 000000000..a5c644953 --- /dev/null +++ b/tests/CacheClearSingleInstance.test.php @@ -0,0 +1,44 @@ + https://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ + +use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; +use Phpfastcache\CacheManager; +use Phpfastcache\Exceptions\PhpfastcacheInstanceNotFoundException; +use Phpfastcache\Helper\TestHelper; + +chdir(__DIR__); +require_once __DIR__ . '/../vendor/autoload.php'; +$testHelper = new TestHelper('Clear single cache instance'); +$defaultDriver = (!empty($argv[1]) ? \ucfirst($argv[1]) : 'Files'); +$instanceId = str_shuffle(md5(\time() - \random_int(0, 86400))); +$instanceId2 = str_shuffle(md5(\time() - \random_int(0, 86400))); +$instanceId3 = str_shuffle(md5(\time() - \random_int(0, 86400))); +$instanceId4 = str_shuffle(md5(\time() - \random_int(0, 86400))); + +$driverInstance = CacheManager::getInstance($defaultDriver, null, $instanceId); +$driverInstance2 = CacheManager::getInstance($defaultDriver, null, $instanceId2); +$driverInstance3 = CacheManager::getInstance($defaultDriver, null, $instanceId3); +$driverInstance4 = CacheManager::getInstance($defaultDriver, null, $instanceId4); + +CacheManager::clearInstance($driverInstance2); + +$cacheInstances = CacheManager::getInstances(); +if(\count($cacheInstances) === 3){ + $testHelper->printPassText('A single cache instance have been cleared'); +}else{ + $testHelper->printFailText('A single cache instance have NOT been cleared'); +} + +$driverInstance2Hash = spl_object_hash($driverInstance2); +foreach ($cacheInstances as $cacheInstance) { + $driverInstanceHash = spl_object_hash($cacheInstance); + if($driverInstanceHash !== $driverInstance2Hash){ + $testHelper->printPassText("Compared cache instance #{$driverInstanceHash} does not match with previously cleared cache instance #" . $driverInstance2Hash); + }else{ + $testHelper->printFailText("Compared cache instance #{$driverInstanceHash} unfortunately match with previously cleared cache instance #" . $driverInstance2Hash); + } +} \ No newline at end of file