python - Django 使用 Redis 进行缓存

标签 python django redis django-cache django-redis

我已经按照此博客使用redis实现了django缓存:https://realpython.com/caching-in-django-with-redis/

所以我按照这个安装了这个包, 添加于

CACHES = {
"default": {
    "BACKEND": "redis_cache.RedisCache",
    "LOCATION": "redis://127.0.0.1:8000/",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient"
    },
    "KEY_PREFIX": "example"
}

}

然后在 View 中。

from django.conf import settings
from django.core.cache.backends.base import DEFAULT_TIMEOUT
from django.views.decorators.cache import cache_page

CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)

然后添加函数的装饰器

@cache_page(CACHE_TTL)
@login_required_dietitian
def patient_profile(request, id):
    data = {}
    return render(request, 'profile.html', {'data':data})

然后我在运行服务器时收到此错误

redis.exceptions.ConnectionError: Connection closed by server.

我对这种缓存技术不熟悉,有什么建议如何解决这个问题吗?

最佳答案

您的配置指定 Redis 在端口 8000 上运行,默认情况下,Redis 在端口 6379 上运行。看起来它正在尝试连接您的 Django 应用程序,因此出现连接错误。 Redis 作为一个单独的进程运行,监听端口 6379 上的请求。

关于python - Django 使用 Redis 进行缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58135139/

相关文章:

python - XmbTextListToTextProperty 结果代码 -2 : error during executing cv2. imshow 来自 shell 脚本的 .py 文件

python - Python代码中的Cython代码

python - 用于 Django 或 Python 的 Ninja-IDE

mysql - 有了MySQL临时表,为什么还要Redis?

python exec 在 2.7 和 3.3 之间表现不同

python - 推荐的 Python Atom 提要生成器?

Django 迁移 RunPython 无法调用模型方法

python - celery 任务不适用于 gevent

performance - Redis 中的 Lua 脚本 : HMGET with table?

redis - 在 Ignite 上使用 Redis 协议(protocol)