将键缓存为“数组”或“对象” - Laminas Cache with Doctrine (PHP8.3)

问题描述 投票:0回答:1

问题也已发布在这里: https://discourse.laminas.dev/t/laminas-cache-with-doctrine-doctrine-orm-6-1-laminas-cache-3-12-php-8-3/3780

PHP8.3 上的全新安装。使用以下软件包:

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

配置:

   ...
    '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,
            ]
        ]
    ],
   ...

“服务/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;
    }
}

工作正常。连接到 Redis 实例并创建密钥。但是,键存储为“数组”和“对象”作为字符串,并且始终执行查询而不是从缓存中获取(我认为存储的数组/对象字符串无论如何都不会起作用)。

Cached key values 你能帮我找出我可能做错了什么吗?
或者,您对最近的 Doctrine/Laminas/Cache 实现有什么建议吗(除了适用于 Laminas 文档的 Doctrine ORM 模块,该模块适用于 v5,但不完全适用于 v6)?

提前致谢。

caching redis doctrine-orm laminas php-8.3
1个回答
0
投票

当然是“序列化器”。

显然,序列化器必须使用 lib_options 参数显式设置。以下是 IGBINARY 序列化器。也可以设置为 'lib_options' => [1 => 2]。

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

希望这个问题可以帮助尝试使用 Doctrine ORM 6 和 Laminas Cache 3 设置缓存的人。

© www.soinside.com 2019 - 2024. All rights reserved.