Reputation: 1191
I would like to use Redis 6.0.1
in my Symfony 5.0.8
project for caching Doctrine 2
queries and result. I tried to configure Redis for Doctrine in cache.yaml
and doctrine.yaml
after added predis/predis
package, but I'm not sure my configuration is correct.
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: 'App\Entity'
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
Does my configuration for Redis seem correct to you? And how can I test if Redis works in my project?
Thanks in advance
Upvotes: 2
Views: 4337
Reputation: 1191
I finally succeeded, I give the solution for those who would be in the same case as me:
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: 'App\Entity'
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
In a Doctrine Repository I created this function that I use in all repository functions:
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 ###
Upvotes: 6