使用redis缓存doctrine 2查询并生成symfony 5

bfrts1fy  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(426)

我想用 Redis 6.0.1 在我的 Symfony 5.0.8 用于缓存的项目 Doctrine 2 查询和结果。我试着把redis配置成 cache.yaml 以及 doctrine.yaml 添加后 predis/predis 包,但我不确定我的配置是否正确。 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

你觉得我的redis配置正确吗?我如何测试redis在我的项目中是否工作?
提前谢谢
w6mmgewl

w6mmgewl1#

我终于成功了,我为那些和我一样的人提供了解决方案:
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

条令.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

在一个条令存储库中,我创建了一个用于所有存储库功能的函数:

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.本地:


### > Redis ###

REDIS_HOST=redis
REDIS_PORT=6379

### > Redis ###

相关问题