安装django-redis

pip3 install django-redis

INSTALLED_APPS中添加django_redis

添加缓存配置

CACHES = {
"default": {
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://127.0.0.1:6379/1",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        "PASSWORD": "Qwerty123_dev"
    }
},
"session":{
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://127.0.0.1:6379/2",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
    }
}

}

将磁盘session改为内存session,所以在settings中添加

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = "session"

标签: django, drf, restframework, redis, cache

添加新评论