在具有中央ElastiCache(redis)羣集的多個AWS EC2實例上運行分佈式SF3.3應用程序。具有Doctrine 2二級緩存和Symfony 3.3的多區域緩存
每個EC2實例還運行用於Doctrine元和查詢緩存的本地Redis實例。
此應用程序使用Doctrines Second Level Cache,從功能的角度來看,它工作得非常好。但是AWS上的性能很差(900-1200毫秒頁面加載),這是因爲它在我們的許多頁面上需要加載的我們的Country和VatRate實體所進行的400多次緩存調用。
由於這些Country和VatRate實體很少發生變化,我想通過使用二級緩存中定義的不同區域來利用本地Redis實例和ElastiCache進行結果緩存。這應該可以減少400+緩存調用的延遲問題,因爲在單個頁面加載時運行時間小於100毫秒。閱讀文檔這一切似乎都是可能的,只是不完全確定如何使用Symfony和PHP-Cache進行配置。
當前配置的一個例子:
應用/配置/ 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 /實體/ 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 /實體/ 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;
// ...
}
嘗試配置
應用程序/配置/ 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 /實體/ Country.php
/**
* @ORM\Table(name = "countries")
* @ORM\Cache(usage = "READ_ONLY", region = "local")
*/
class Country
{
// as above
}
的src /的appbundle /實體/ VatRate.php
/**
* @ORM\Table(name = "vatRates")
* @ORM\Cache(usage = "READ_ONLY", region = "local")
*/
class VatRate
{
// as above
}
的src /的appbundle /實體/ 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但缺乏配置文件,這使得它更具挑戰性!
謝謝,問題是試圖正確地創建這些服務。正如我對待嘗試的配置 - app/config/config.yml的todo評論中所述 – Nick