我的 symfony 应用程序有多个实例在单独的 docker 容器中运行。
并且我已经配置了我的 app.cache
以使用 redis:
framework:
cache:
app: cache.adapter.redis
我有相同的prefix_seed
:
framework:
cache:
prefix_seed: 'dev'
结果我在 redis 中得到了这样的东西:
1605259288.470950 [0 172.18.0.28:55044] "MGET" "HnMEIyUlZ+:workers.restart_requested_timestamp"
1605259288.471680 [0 172.18.0.28:55044] "SET" "HnMEIyUlZ+:workers.restart_requested_timestamp" "d:1605259288.471522;"
1605259314.483389 [0 172.18.0.29:42884] "MGET" "8TMgMtnOAG:workers.restart_requested_timestamp"
正如您从上面看到的,2 个不同的实例试图通过相同的键 workers.restart_requested_timestamp
从 Redis 中获取值,但是即使使用相同的 prefix_seed
,前缀也不同>.
在这个例子中,我使用了 messenger 组件,我想通过 stop-workers
命令(通过共享的 redis)停止到处运行的 worker。但一般来说这与缓存配置有关。
如何克服这个问题并告诉两个应用程序使用同一个池?这是什么配置?
最佳答案
最后,我找到了解决方案。有一个选项可以创建您自己的带有映射适配器的缓存池。这里的主要技巧是将带有 namespace
的标签传递给您的适配器(名称也应该是 cache.pool
):
framework:
cache:
pools:
cache.redis_shared_pool:
adapter: app.cache_shared_redis_adapter
services:
app.cache_shared_redis_adapter:
parent: 'cache.adapter.redis'
tags:
- { name: 'cache.pool', namespace: 'shared' }
就是这样!您在 Redis 中的所有键都将以 shared:
为前缀。现在您应该将您的 @cache.redis_shared_pool
传递给您想要的任何服务。
至于信使组件,我们应该覆盖服务(但我不确定这是最好的方法):
console.command.messenger_stop_workers:
class: Symfony\Component\Messenger\Command\StopWorkersCommand
arguments:
$restartSignalCachePool: "@cache.redis_shared_pool"
messenger.listener.stop_worker_on_restart_signal_listener:
class: Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener
arguments:
$cachePool: "@cache.redis_shared_pool"
关于php - 如何在多个 Symfony 实例之间共享应用程序缓存(共享缓存池)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64818116/