Skip to content
Merged
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
25 changes: 20 additions & 5 deletions lib/Phpfastcache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/CacheClearSingleInstance.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*/

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