How to use Filesystem Cache in Mezzio?

namespace Auth\Service\Factory;

use Auth\Service\RbacManager;
use Doctrine\ORM\EntityManager;
use Laminas\Cache\Storage\Adapter\Filesystem;
use Laminas\Cache\Storage\Plugin\ExceptionHandler;
use Laminas\Cache\Storage\Plugin\Serializer;
use Laminas\Cache\StorageFactory;
use Psr\Container\ContainerInterface;

class RbacManagerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $cache = StorageFactory::factory([
            'adapter' => [
                'name' => Filesystem::class,
                'options' => [
                    'namespace' => 'filesystem',
                    'cache_dir' => './data/cache/AuthRbac',
                    'ttl' => 60 * 60 * 10
                ],
            ],
            'plugins' => [
                Serializer::class,
                ExceptionHandler::class => [
                    'throw_exceptions' => false,
                ],
            ],
        ]);

        return new RbacManager(
            $container->get(EntityManager::class),
            $cache
        );
    }
}

I get an error:

Error: Class 'Laminas\Serializer\Serializer' not found in file /var/www/vendor/laminas/laminas-cache/src/Storage/Plugin/PluginOptions.php on line 214

Stack trace:
1. Error->() /var/www/vendor/laminas/laminas-cache/src/Storage/Plugin/PluginOptions.php:214
2. Laminas\Cache\Storage\Plugin\PluginOptions->getSerializer() /var/www/vendor/laminas/laminas-cache/src/Storage/Plugin/Serializer.php:101
3. Laminas\Cache\Storage\Plugin\Serializer->onWriteItemPre() /var/www/vendor/laminas/laminas-eventmanager/src/EventManager.php:331
4. Laminas\EventManager\EventManager->triggerListeners() /var/www/vendor/laminas/laminas-eventmanager/src/EventManager.php:180
5. Laminas\EventManager\EventManager->triggerEvent() /var/www/vendor/laminas/laminas-cache/src/Storage/Adapter/AbstractAdapter.php:206
6. Laminas\Cache\Storage\Adapter\AbstractAdapter->triggerPre() /var/www/vendor/laminas/laminas-cache/src/Storage/Adapter/AbstractAdapter.php:669
7. Laminas\Cache\Storage\Adapter\AbstractAdapter->setItem() /var/www/vendor/laminas/laminas-cache-storage-adapter-filesystem/src/Filesystem.php:844
8. Laminas\Cache\Storage\Adapter\Filesystem->setItem() /var/www/src/Auth/src/Service/RbacManager.php:67

If you use the serializer plugin, you must also install the serializer itself.

1 Like

Yep that should do the trick:

composer require laminas/laminas-serializer
2 Likes