gpt4 book ai didi

php - 具有 Doctrine 2 二级缓存和 Symfony 3.3 的多区域缓存

转载 作者:行者123 更新时间:2023-12-03 01:52:37 26 4
gpt4 key购买 nike

在具有中央 ElastiCache (redis) 集群的多个 AWS EC2 实例上运行分布式 SF3.3 应用程序。

每个 EC2 实例还运行一个本地 Redis 实例,用于 Doctrine 元和查询缓存。

该应用程序利用 Doctrines 二级缓存,从功能角度来看,它工作得非常好。但 AWS 上的性能很差(页面加载时间为 900-1200 毫秒),因为它在我们的国家/地区和许多页面所需的 VatRate 实体中加载了 400 多个缓存调用。

由于这些国家/地区和增值税率实体很少发生变化,我想通过使用二级缓存中定义的不同区域来利用本地 Redis 实例和 ElastiCache 进行结果缓存。这应该可以减少 400 多个缓存调用的延迟问题,因为在单个框上运行时页面加载时间低于 100 毫秒。阅读文档,这一切似乎都是可能的,只是不完全确定如何使用 Symfony 和 PHP-Cache 配置它。

当前配置的示例:

app/config/config.yml

doctrine:
dbal:
# .. params

orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
second_level_cache:
enabled: true
region_cache_driver:
type: service
id: doctrine.orm.default_result_cache

cache_adapter:
providers:
meta: # Used for version specific
factory: 'cache.factory.redis'
options:
host: 'localhost'
port: '%redis_local.port%'
pool_namespace: "meta_%hash%"
result: # Used for result data
factory: 'cache.factory.redis'
options:
host: '%redis_result.host%'
port: '%redis_result.port%'
pool_namespace: result

cache:
doctrine:
enabled: true
use_tagging: true
metadata:
service_id: 'cache.provider.meta'
entity_managers: [ default ]
query:
service_id: 'cache.provider.meta'
entity_managers: [ default ]
result:
service_id: 'cache.provider.result'
entity_managers: [ default ]

src/AppBundle/Entity/Country.php

/**
* @ORM\Table(name = "countries")
* @ORM\Cache(usage = "READ_ONLY")
*/
class Country
{
// ...

/**
* @var VatRate
*
* @ORM\OneToMany(targetEntity = "VatRate", mappedBy = "country")
* @ORM\Cache("NONSTRICT_READ_WRITE")
*/
private $vatRates;

// ...
}

src/AppBundle/Entity/VatRate.php

/**
* @ORM\Table(name = "vatRates")
* @ORM\Cache(usage = "READ_ONLY")
*/
class VatRate
{
// ...

/**
* @var Country
*
* @ORM\ManyToOne(targetEntity = "Country", inversedBy = "vatRates")
* @ORM\JoinColumn(name = "countryId", referencedColumnName = "countryId")
*/
private $country;

// ...
}

src/AppBundle/Entity/Order.php

/**
* @ORM\Table(name = "orders")
* @ORM\Cache(usage = "NONSTRICT_READ_WRITE")
*/
class Order
{
// ...

/**
* @var Country
*
* @ORM\ManyToOne(targetEntity = "Country")
* @ORM\JoinColumn(name = "countryId", referencedColumnName = "countryId")
*/
private $country;
// ...
}

尝试的配置

app/config/config.yml

doctrine:
dbal:
# .. params

orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
second_level_cache:
enabled: true
region_cache_driver: array
regions:
local:
type: service
service: "doctrine.orm.default_result_cache" # TODO: needs to be local redis
remote:
type: service
service: "doctrine.orm.default_result_cache" # TODO: needs to be remote redis

cache_adapter:
providers:
meta: # Used for version specific
factory: 'cache.factory.redis'
options:
host: 'localhost'
port: '%redis_local.port%'
pool_namespace: "meta_%hash%"
result: # Used for result data
factory: 'cache.factory.redis'
options:
host: '%redis_result.host%'
port: '%redis_result.port%'
pool_namespace: result

cache:
doctrine:
enabled: true
use_tagging: true
metadata:
service_id: 'cache.provider.meta'
entity_managers: [ default ]
query:
service_id: 'cache.provider.meta'
entity_managers: [ default ]
result:
service_id: 'cache.provider.result'
entity_managers: [ default ]

src/AppBundle/Entity/Country.php

/**
* @ORM\Table(name = "countries")
* @ORM\Cache(usage = "READ_ONLY", region = "local")
*/
class Country
{
// as above
}

src/AppBundle/Entity/VatRate.php

/**
* @ORM\Table(name = "vatRates")
* @ORM\Cache(usage = "READ_ONLY", region = "local")
*/
class VatRate
{
// as above
}

src/AppBundle/Entity/Order.php

/**
* @ORM\Table(name = "orders")
* @ORM\Cache(usage = "NONSTRICT_READ_WRITE", region = "remote")
*/
class Order
{
// as above
}

结果

Type error: Argument 1 passed to Doctrine\ORM\Cache\DefaultCacheFactory::setRegion() must be an instance of Doctrine\ORM\Cache\Region, instance of Cache\Bridge\Doctrine\DoctrineCacheBridge given,

不太确定从这里去哪里,一直在进行这里的测试:https://github.com/doctrine/DoctrineBundle/blob/74b408d0b6b06b9758a4d29116d42f5bfd83daf0/Tests/DependencyInjection/Fixtures/config/yml/orm_second_level_cache.yml但缺乏配置文档使其变得更具挑战性!

最佳答案

经过多次使用 PHP-Cache 库后,通过查看 CacheBundle 编译器可以清楚地看出,它在配置中仅支持一个 DoctrineBridge 实例。 https://github.com/php-cache/cache-bundle/blob/master/src/DependencyInjection/Compiler/DoctrineCompilerPass.php

解决方案是创建我自己的编译器,虽然不太漂亮,但似乎可以工作。

src/AppBundle/DependencyInjection/Compiler/DoctrineCompilerPass.php

namespace AppBundle\DependencyInjection\Compiler;

use Cache\Bridge\Doctrine\DoctrineCacheBridge;
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class DoctrineCompilerPass implements CompilerPassInterface
{
/** @var ContainerBuilder */
private $container;

public function process(ContainerBuilder $container)
{
$this->container = $container;

$this->enableDoctrineCache('local');
$this->enableDoctrineCache('remote');
}

private function enableDoctrineCache(string $configName)
{
$typeConfig = [
'entity_managers' => [
'default'
],
'use_tagging' => true,
'service_id' => 'cache.provider.' . $configName
];

$bridgeServiceId = sprintf('cache.service.doctrine.%s.entity_managers.bridge', $configName);

$this->container->register($bridgeServiceId, DoctrineCacheBridge::class)
->setFactory([DoctrineBridgeFactory::class, 'get'])
->addArgument(new Reference($typeConfig['service_id']))
->addArgument($typeConfig)
->addArgument(['doctrine', $configName]);
}
}

src/AppBundle/AppBundle.php

use AppBundle\DependencyInjection\Compiler\DoctrineCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new DoctrineCompilerPass());
}
}

app/config/config.yml

doctrine:
dbal:
# ... params

orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
second_level_cache:
enabled: true
regions:
remote:
cache_driver:
type: service
id: cache.service.doctrine.remote.entity_managers.bridge
local:
cache_driver:
type: service
id: cache.service.doctrine.local.entity_managers.bridge

cache_adapter:
providers:
local:
factory: 'cache.factory.redis'
options:
host: '%redis_local.host%'
port: '%redis_local.port%'
pool_namespace: "local_%hash%"
remote:
factory: 'cache.factory.redis'
options:
host: '%redis_result.host%'
port: '%redis_result.port%'
pool_namespace: 'result'

cache:
doctrine:
enabled: true
use_tagging: true
metadata:
service_id: 'cache.provider.local'
entity_managers: [ default ]
query:
service_id: 'cache.provider.local'
entity_managers: [ default ]

虽然这似乎在某种程度上有效,但当缓存中可能缺少某些内容时,本地缓存调用会出现一些不一致,从而导致 500 错误。总的来说,我认为我正在尝试比设计的更多地弯曲二级缓存。

关于php - 具有 Doctrine 2 二级缓存和 Symfony 3.3 的多区域缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46445707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com