Skip to content

[V6˖] Act on all instances

Georges.L edited this page Nov 26, 2016 · 2 revisions

Sometimes you may need to make an operation over all instanced drivers. As of the V6 an helper is now available to make those operations easier:

Getting statistics of all instanced drivers:

<?php

/**
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
 * @author Georges.L (Geolim4)  <[email protected]>
 */
use phpFastCache\CacheManager;
use phpFastCache\Entities\driverStatistic;
use phpFastCache\Helper\ActOnAll;


chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';

$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
$status = 0;
echo "Testing ActOnAll helper\n";

/**
 * Testing memcached as it is declared in .travis.yml
 */
$filesInstance = CacheManager::getInstance('Files');
$RedisInstance = CacheManager::getInstance('Redis');
$MemcacheInstance = CacheManager::getInstance('Memcached');

$actOnAll = new ActOnAll();
$statsAry = $actOnAll->getStats();

As you can see, $statsAry will contains an array of driver's statistics. This is not the only use case, you can also delete many item by their name:

<?php

/**
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
 * @author Georges.L (Geolim4)  <[email protected]>
 */
use phpFastCache\CacheManager;
use phpFastCache\Entities\driverStatistic;
use phpFastCache\Helper\ActOnAll;


chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';

$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
$status = 0;
echo "Testing ActOnAll helper\n";

/**
 * Testing memcached as it is declared in .travis.yml
 */
$filesInstance = CacheManager::getInstance('Files');
$RedisInstance = CacheManager::getInstance('Redis');
$MemcacheInstance = CacheManager::getInstance('Memcached');

$actOnAll = new ActOnAll();
$actOnAll->deleteItem('ItemName');

The full list of available methods is available here.

Clone this wiki locally