Loading

django中使用redis

启动redis
1.下载django-redis模块

pip install django-redis

2.settings.py

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",  # 安装redis的主机的 IP 和 端口
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {
                "max_connections": 1000,
                "encoding": 'utf-8'
            },
            "PASSWORD": "qwe123"  # redis密码
        }
    }
}

3.使用示例

from django_redis import get_redis_connection
        conn = get_redis_connection("default")
        key = f"xiaohe_register_{mobile}"
        conn.set(key, random_code, ex=60 * 5)

        value = conn.get(key)
        print("测试读取", value)
posted @ 2024-10-05 01:42  一只大学生  阅读(26)  评论(0编辑  收藏  举报