cachingredisdoctrine-ormlaminasphp-8.3

Cached keys as "Array" or "Object" - Laminas Cache with Doctrine (PHP8.3)


The question has been also posted here: https://discourse.laminas.dev/t/laminas-cache-with-doctrine-doctrine-orm-6-1-laminas-cache-3-12-php-8-3/3780

Clean installation on PHP8.3. Using the following packages:

"doctrine/doctrine-orm-module": "^6.1",
"doctrine/doctrine-module": "^6.1",
"laminas/laminas-cache":"^3.12",
"laminas/laminas-cache-storage-adapter-redis":"^2.9"

Configuration:

   ...
    'service_manager' => [
        'factories' => [
            'Service\Redis' => Service\Factory\RedisFactory::class,
        ],
    ],
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => \Doctrine\DBAL\Driver\PDO\MySQL\Driver::class,
                'params' => [
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'db_user',
                    'password' => 'my_password',
                    'dbname'   => 'db_skeleton',
                    'charset'  => 'utf8mb4',
                ],
            ],
        ],
        'driver' => [
            'annotation_driver' => [
                'class' => AttributeDriver::class,
                'cache' => 'array',
                'paths' => [
                    __DIR__ . '/../src/Entity',
                ],
            ],
            'orm_default' => [
                'drivers' => [
                    'Application\Entity' => 'annotation_driver',
                ],
            ],
        ],
        'cache' => [
            'redis' => [
                'namespace' => 'skeleton',
                'instance'  => 'Service\Redis',
            ],
        ],
        'configuration' => [
            'orm_default' => [
                'result_cache'      => 'redis',
                'query_cache'       => 'redis',
                'metadata_cache'    => 'redis',
                'hydration_cache'   => 'redis',

                'generate_proxies'  => false,
            ]
        ]
    ],
   ...

"Service/Redis" -> RedisFactory.php

<?php
namespace Application\Service\Factory;
use Laminas\Cache\Storage\Adapter\Redis;
use Psr\Container\ContainerInterface;
class RedisFactory
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $redis = new Redis([
            'server' => ['host' => '127.0.0.1', 'port' => 6379],
        ]);

        return $redis;
    }
}

Works fine. Connecting to the Redis instance and creating the keys. However, the keys are stored as "Array" and "Object" as string and the query is always executed instead of fetching from the cache (which I don't think the stored array/object strings will work anyway).

Cached key values Could you help me figure out what I might be doing wrong?
Or, do you have any recommendations for a recent Doctrine/Laminas/Cache implementation (aside from the Doctrine ORM Module for Laminas documentation, which works with v5 but not fully with v6)?

Thanks in advance.


Solution

  • Of course, the "serializer".

    Apparently, the serializer must be explicitly set with the lib_options parameter. The following is for IGBINARY serializer. It could be also set as 'lib_options' => [1 => 2].

    $redis->setOptions([
        'server' => ['host' => '127.0.0.1', 'port' => 6379, 'timeout' => 3600],
        'lib_options' => [\Redis::OPT_SERIALIZER => \Redis::SERIALIZER_IGBINARY]
    ]);
    

    Hope this question helps someone trying to set up cache with Doctrine ORM 6 and Laminas Cache 3.