我們有一個(gè)新的 Symfony 設(shè)置,以 Redis 作為快取機(jī)制。我們想要連接到特定主機(jī),而不是預(yù)設(shè)的本機(jī)。在生產(chǎn)環(huán)境中,./bin/console debug:dotenv 給了正確的 REDIS_HOST。這是在我們的 .env 和 .env.local.php 中配置的。
我們得到的錯(cuò)誤是:
連線被拒絕:tcp:127.0.0.1/6379
這是我們的設(shè)定:
services.yml
#services: Redis: # you can also use \RedisArray, \RedisCluster or \Predis\Client classes class: \Predis\Client calls: - connect: - '%env(REDIS_HOST)%' - '%env(int:REDIS_PORT)%' Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: arguments: - '@Redis' - prefix: sp_ss_ - ttl: 1800
cache.yml
#framework: cache: app: cache.adapter.redis default_redis_provider: 'Redis' pools: site.cache: adapter: cache.app
以及我們的 .env 檔案:
APP_ENV=prod APP_SECRET=**** MESSENGER_TRANSPORT_DSN=redis://redis.local:6379/messages REDIS_HOST=redis.local REDIS_PORT=6379 REDIS_URL=redis://redis.local:6379
Symfony 的文件建議使用“calls -> connect”,但只有當(dāng)您將類別定義為“Redis”時(shí)才使用它。當(dāng)您使用'\Predis\Client'時(shí),您需要使用以下設(shè)定:
「config/services.yaml」
Redis: # you can also use \RedisArray, \RedisCluster or \Predis\Client classes class: \Predis\Client # See all parameters here: https://github.com/predis/predis/wiki/Connection-Parameters#list-of-connection-parameters arguments: - host: '%env(REDIS_HOST)%' - port: '%env(int:REDIS_PORT)%' # uncomment the following if your Redis server requires a password # - password: '%env(REDIS_PASSWORD)%'
我還使用“\Predis\Client”,更改為“arguments”後,連接在這裡工作。
有關(guān)更多參數(shù)參考,請(qǐng)查看此連結(jié)(連接參數(shù)清單).