python - Django 中的 Redis : redis. exceptions.ConnectionError : Error -2 connecting to 127. 0.0.1 :20789:0. 名称或服务未知

标签 python django redis

我正在使用 django-cache-redis,但我陷入了以下悖论:

# in `python manage.py shell`
import redis
r = redis.StrictRedis(host='127.0.0.1', port=20789, db=0)
r.set('foo', 'bar')  # returns True

from django.core.cache import cache
cache.set('foo', 'bar', 1)  # raises redis.exceptions.ConnectionError

Traceback (most recent call last): redis.exceptions.ConnectionError: Error -2 connecting to 127.0.0.1:20789:0. Name or service not known.

请注意主机、端口和数据库在这两种情况下都是相同的。

使用import redis已经是debug;使用 redis-ctl 也可以。我还尝试根据 this question127.0.0.1 更改为 localhost , 没有成功。

知道这可能是什么吗?

我的 CACHES 配置已经是最小的了:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.cache.RedisCache',
        'LOCATION': '127.0.0.1:20789:0',
        'TIMEOUT': 60*60*24,
    }
}

最佳答案

我猜,但你有:

'LOCATION': '127.0.0.1:20789:0'

而 django-redis-cache 的自述文件指定:

'LOCATION': '<host>:<port>'

即没有 :<db>最后

https://github.com/sebleier/django-redis-cache

关于python - Django 中的 Redis : redis. exceptions.ConnectionError : Error -2 connecting to 127. 0.0.1 :20789:0. 名称或服务未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27951678/

相关文章:

python - 在 qgs Composer 图例中仅显示过滤层 - PyQgis

python - Django session - 在登录/注销之间保存 session 数据

python - Python 中有一个迭代字典

redis - 当 Redis 被驱逐到可用内存或过期时,它可以转储 key 吗

java - 使用 Protocol Buffers 时,这里可能会导致 java.lang.OutOfMemoryError 的原因是什么?

python - 将逗号分隔值转换为字典

python - Django 子查询

python - 如何在Django运行时中进行更改?

django - “WSGIRequest”对象没有属性 'sessions' (Django 2.1.2)

redis - 如何连接到远程Redis服务器?