Implementing redis cache in laminas mvc application

I have installed php redis in my local system and have added the redis adapter via the composer command: composer require laminas/laminas-cache-storage-adapter-redis

I tried following this documentation: Adapters - laminas-cache - Laminas Docs

I am having trouble setting up the service and the factory files for the same

So far I have defined the options in a config file ‘redis.local.php’:

return [
    'redis-cache' => [
        'adapter' => [
            'name' => 'redis',
            'options' => [
                'ttl' => 3600,
                'server' => [
                    'host' => '127.0.0.1',
                    'port' => 6379,
                ],
            ],
        ],
        'plugins' => [
            [
                'name' => 'exception_handler',
                'options' => [
                    'throw_exceptions' => true,
                ],
            ],
        ],
    ]
];

and tried to add the options via the factory:

class RedisCacheServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): RedisCacheService
    {
        $config = $container->get('config');

        return new RedisCacheService($config['redis-cache']);
    }
} 

and then try to typecast it as a StorageInterFace object (might be doing something wrong here):

class RedisCacheService
{
    public function __construct(private StorageInterface $cacheStorage)
    {
    }
}

Please note: I am new to laminas, I also see an implementation where redis cache is coupled with doctrine.

Hello and welcome to our forums! :smiley:

Have you followed the instructions in the documentation?

Thanks for replying and apologies for the delayed reply on my end.

Regarding the documentation link you have posted, the controller setup in my project a bit differently, but I have still included the configuration in a similar. The controllers are registered in the config and ‘listeners’ (having file names ending with keyword ‘Resource’) are used to write controller logic instead of the controller files themselves

I have made some changes with regards to the cache mechanism, which I will post about in a following reply

I was getting the error ‘Class "Redis" not found’ error. I just realized the php redis extension was not enabled, which I found out after checking phpinfo(). I was able to go on to the next step after enabling the extension. I will make sure to post a pseudo code either here or as a separate post so that it may help others