Qquestion about Phpfastcache and DI container

Hello,

A quick question if anyone tried to use Phpfastcache & php-di? I decided speed-up the overall Slim-based application performance added to my container definitions file following lines:

return [
   
	GuzzleClient::class => function (ContainerInterface $container) {
		$c = $container->get(Configuration::class);
		$optGuzzle = array();

		if ($c->findString('guzzle.cert') && !empty($c->getString('guzzle.cert'))) {
			$optGuzzle['verify'] = $c->getString('app.basePath') . $c->getString('guzzle.cert');
		}
		if ($c->findArray('guzzle.proxy') && !empty($c->getArray('guzzle.proxy'))) {
			$optGuzzle['proxy'] = $c->getArray('guzzle.proxy');
		}

		return new GuzzleClient($optGuzzle);
	},

	\Phpfastcache\CacheManager::class => function (ContainerInterface $container) {
		return \Phpfastcache\CacheManager::getInstance('Files', new \Phpfastcache\Config\ConfigurationOption(['path' => 'c:/jwk/cache',]));
	},
]

I’m using a pretty standard pattern, but for some reason the code above always fail with an error :

Fatal error: Class Phpfastcache\CacheManager contains 9 abstract methods and must therefore be declared abstract or implement the remaining methods (Psr\Cache\CacheItemPoolInterface::getItem, Psr\Cache\CacheItemPoolInterface::getItems, Psr\Cache\CacheItemPoolInterface::hasItem, …) in
C:_ng\geonosis\nginx-php-fpm-aws\src\vendor\phpfastcache\phpfastcache\lib\Phpfastcache\CacheManager.php on line 35

CacheManager class itself doesn’t have any abstract methods … Any ideas how to get it containerized in a proper way?

As far as I can see, the CacheManager is just a “factory” class that creates the driver specific cache instance and also a singleton at the same time.

I would recommend defining an DI container definition for the PSR-16 Psr\SimpleCache\CacheInterface instead.

Example:

use Phpfastcache\Helper\Psr16Adapter;
use Psr\SimpleCache\CacheInterface;
use Phpfastcache\Config\ConfigurationOption;
// ...

CacheInterface::class => function (ContainerInterface $container) {
    $option = new ConfigurationOption(['path' => 'c:/jwk/cache']);

    return new Psr16Adapter('Files', $option);
},
1 Like