Skip to content

[V7˖] Override a core driver | Setup a custom driver

Georges.L edited this page Apr 7, 2018 · 6 revisions

🆕 As of the V7 there is a cleaner way to use a custom driver or to override a core driver.

Adding a custom driver

This feature is pretty easy actually:

<?php
use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Fakefiles\Config;

$driverName = 'Fakefiles';
$className = \Phpfastcache\Drivers\Fakefiles\Driver::class;
CacheManager::addCustomDriver($driverName, $className);
$cacheInstance = CacheManager::getInstance($driverName, new Config(['customOption' => true]));

The only requirement is that $className MUST implements \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface.

Once you done you can remove * the custom driver easily:

<?php
use Phpfastcache\CacheManager;
CacheManager::removeCustomDriver('Fakefiles');

Overriding custom driver

This feature is pretty close of adding a custom driver, except that $driverName MUST be an existing core driver.

<?php
use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Fakefiles\Config;

$driverName = 'Files';
$className = \Phpfastcache\DriverTest\Files2\Driver::class;
CacheManager::addCoreDriverOverride($driverName, $className);
$cacheInstance = CacheManager::getInstance($driverName, new Config(['customOption' => true]));

However, here $className MUST extends the base class provided by the Phpfastcache core (which implicitly implements \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface).

Once you done you can remove * the driver override easily:

<?php
use Phpfastcache\CacheManager;
CacheManager::removeCoreDriverOverride('Files');

* Please note that removing overrides/custom drivers only apply to the next cache instance generated by the cacheManager. It will NOT update actual instance that have been already generated.

⚠️ Finally, the old way to use a custom driver as per the documentation that can be found here using CacheManager::setNamespacePath() is now deprecated.

Clone this wiki locally