【问题标题】:Doctrine connect to redis WITH password教义使用密码连接到redis
【发布时间】:2015-11-05 22:32:45
【问题描述】:

听起来很简单,但它让我坚持了好几个小时。

如何使用密码连接到 redis 缓存服务器。它在没有密码的情况下成功连接和缓存学说查询,但是当我输入密码时它会抛出异常

InvalidConfigurationException in ArrayNode.php line 309:
Unrecognized option "password" under "doctrine.orm.entity_managers.default.query_cache_driver"

我尝试了一个组合,但我当前的代码是 配置.yml .....

    entity_managers:
        default:
            metadata_cache_driver: apc
            query_cache_driver:
                type: redis
                host: localhost
                port: 6379
#                password: myStr0nG!passw0rd - adding this causes exception
                instance_class: Redis
            result_cache_driver:
                type: redis
                host: localhost
                port: 6379
#                password: myStr0nG!passw0rd - adding this causes exception
                instance_class: Redis

【问题讨论】:

  • 嘿,Don Omondi,你能告诉我希望驱动程序是最好的数组、redis、apc memcache 吗??

标签: php symfony caching doctrine-orm redis


【解决方案1】:

您不能为缓存驱动程序设置密码,因为它不受支持。

如果您想要替代方案,请考虑使用SncRedisBundle

示例配置

snc_redis:
    clients:
        cache:
            type: predis
            alias: default
            dsn: redis://secret@localhost/1
            logging: %kernel.debug%
    doctrine:
        metadata_cache:
            client: cache
            entity_manager: default          # the name of your entity_manager connection
            document_manager: default        # the name of your document_manager connection
        result_cache:
            client: cache
            entity_manager: [default, read]  # you may specify multiple entity_managers
        query_cache:
            client: cache
            entity_manager: default

【讨论】:

  • github.com/doctrine/DoctrineCacheBundle/blob/master/… 是您无法设置密码的原因。
  • @Vamsi 感谢您的回答。我之前看过这个包,但并不热衷于接受它,因为它涉及更改我的控制器的代码,这会使从 redis 切换到 memcached 或任何其他困难。我想我现在别无选择。
  • @user2268997 所以,如果我要破解该文件并在显示密码 => null 的地方手动输入我的密码,它会起作用吗?
  • 应该,但请不要因为它会破坏可维护性,我建议你安装docteries-cache-bundle,创建一个redis缓存,并将其作为service类型缓存传递给配置.ie:将由学说缓存包创建的服务传递给学说包。在doctrine cache bundle的文档中也没有支持redis缓存的密码,但我看了一下代码,你应该可以通过该缓存配置的password 键中的密码。
  • @user2268997 好点子和建议。我不喜欢安装许多捆绑包,特别是当标准版具有类似的内置功能时,但在这种特殊情况下,我决定使用我接受的 Vamsi 回答中提到的 SncRedisBundle。
【解决方案2】:

要在没有 Snc 包的情况下使用密码,只需使用教义缓存条目:

在 AppKernel 上添加 DoctrineCacheBundle:

$bundles = [
    ...
    new Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle(),
    ...
];

config.yml

doctrine_cache:
    providers:
        my_provider:
            redis:
                host: hostname
                port: 6379
                password: foobared
doctrine:
    orm:
        metadata_cache_driver:
            type: service
            id: doctrine_cache.providers.my_provider

【讨论】:

    【解决方案3】:

    我以与rogeriolino 类似的方式解决了这个问题。

    这是与当前问题相关的配置部分:

    config.yml

    doctrine:
        dbal:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        orm:
            auto_generate_proxy_classes: "%kernel.debug%"
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: true
            result_cache_driver:
              type: service
              id: "app.my_redis"
            metadata_cache_driver:
              type: service
              id: "app.my_redis"
            query_cache_driver:
              type: service
              id: "app.my_redis"
    

    service.yml

    services:
        app.my_redis:
            class: AppBundle\Utils\Cache\MyRedis
    

    服务类:

    namespace AppBundle\Utils\Cache;
    use Doctrine\Common\Cache\Cache;
    use Doctrine\Common\Cache\RedisCache;
    
    
    class MyRedis extends RedisCache
    {
        private $password = 'some string you like but that passes';
        private $host = "some ip address";
        private $port = 6379;
        private $timeout = 0.0;
        private $dbNumber = 1;
        private $dbMap = [
            'local.windows.domain',
            'production.domain',
            'staging.domain',
        ];
    
        /**
         * MyRedis constructor.
         */
        public function __construct()
        {
            $redis = new \Redis;
            $redis->connect($this->host, $this->port, $this->timeout);
            $redis->auth($this->password);
            // This way of $_SERVER checking could be solved better.
            $redis->select(array_search($_SERVER['HTTP_HOST'], $this->dbMap));
            $this->setRedis($redis);
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果现在有人仍然有问题,您可以提供带有 DSN 格式的用户和密码的连接字符串,例如:

      # DSN format
      redis://USER:PASSWORD@HOST:PORT
      
      # example
      redis://redis_username:redis_password@redis-hostname-or-ip.com:6379
      

      这样你可以,例如,在 Symfony 中定义默认的 redis 提供者

      # 'config/packages/cache.yaml'
      framework:
          cache:
              # better handle secrets via ENV vars, like: %env(resolve:REDIS_DSN)%
              default_redis_provider: 'redis://redis_username:redis_password@redis-hostname-or-ip.com:6379'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        • 2020-12-05
        • 2019-02-09
        • 1970-01-01
        相关资源
        最近更新 更多