使用Redis缓存Doctrine 2查询并在Symfony 5中产生结果



我想在Symfony 5.0.8项目中使用Redis 6.0.1来缓存Doctrine 2查询和结果。在添加了predis/predis包后,我尝试在cache.yamldoctrine.yaml中为Doctrine配置Redis,但我不确定我的配置是否正确。

doctrine.yaml:

doctrine:
dbal:
default_connection: default
connections:
default:
dbname: '%env(DATABASE_NAME)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASSWORD)%'
host: '%env(DATABASE_HOST)%'
driver:  '%env(DATABASE_DRIVER)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'AppEntity'
alias: App
metadata_cache_driver: ~
query_cache_driver:
type: pool
id: doctrine.query_cache_pool
result_cache_driver:
type: pool
id: doctrine.result_cache_pool

cache.yaml:

framework:
cache:
# Redis
app: cache.adapter.redis
default_redis_provider: redis://localhost

你觉得我的Redis配置正确吗?如何测试Redis在我的项目中是否有效?

提前感谢

我终于成功了,我为那些与我处境相同的人提供了解决方案:

cache.yaml:

framework:
cache:
app: cache.adapter.redis
default_redis_provider: "redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%"
pools:
doctrine.result_cache_pool:
adapter: cache.adapter.redis
doctrine.system_cache_pool:
adapter: cache.adapter.redis

doctrine.yaml:

doctrine:
dbal:
default_connection: default
connections:
default:
dbname: '%env(DATABASE_NAME)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASSWORD)%'
host: '%env(DATABASE_HOST)%'
driver:  '%env(DATABASE_DRIVER)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'AppEntity'
alias: App
metadata_cache_driver: ~
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool

在条令库中,我创建了这个函数,我在所有库函数中都使用它:

protected function getQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('article')
->setCacheMode(Cache::MODE_NORMAL)
->setCacheable(true)
->setLifetime(300);
}

docker-compose.yaml:

version: "3.4"
services:
php:
...
depends_on:
- redis
redis:
image: redis:6.0.1
ports:
- 6379:6379
volumes:
- ./docker/redis/redis.conf:/redis.conf
command: [ "redis-server", "/redis.conf" ]

.env.local:

###> Redis ###
REDIS_HOST=redis
REDIS_PORT=6379
###> Redis ###

相关内容

  • 没有找到相关文章

最新更新