我正在尝试使用 requests_cache
和 redis 缓存我的请求,如下所示:
requests_cache.install_cache(
'requests_cache', backend='redis', expire_after=600
)
当 Redis 在 localhost:6379
上运行时,一切正常并且开箱即用。
然而,当我将我的应用程序部署到有 REDIS_URL 环境变量的 Heroku 时,上述命令失败,因为显然 REDIS_URL 没有指向 localhost
:
Error 111 connecting to localhost:6379. Connection refused.
所以问题是,如何让它在 Heroku 上运行?文档在这个问题上并不清楚。
最佳答案
您必须向 install_cache
传递一个名为 connection
的附加参数,它将是 StrictRedis
类型。所以我想像这样创建它:
r = redis.StrictRedis(host='REDIS_URL', port=6379, db=0)
requests_cache.install_cache(
'requests_cache', backend='redis', expire_after=600, connection=r
)
或类似的东西,取决于REDIS_URL
包含多少信息(协议(protocol)、端口等)
关于heroku - 请求缓存 : connecting using REDIS_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46693610/