django-redis配置

django-redis配置

将session保存到redis数据库

django默认将session信息存储到mysql数据库,如果用户访问量增大,会给mysql数据库服务造成非常大的压力。

redis缓存数据库,将数据存储到内存,存取速度快。
django

参考文档:http://django-redis-chs.readthedocs.io/zh_CN/latest/#

  1. 安装
    pip install django-redis
  2. 作为 cache backend 使用配置
 CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://127.0.0.1:6379/1",
            "OPTIONS": {
                "CLIENT_CLASS": "django_redis.client.DefaultClient",
            }
        }
    }

URL 格式举例

redis://[:password]@localhost:6379/0
rediss://[:password]@localhost:6379/0
unix://[:password]@/path/to/socket.sock?db=0
  1. 配置session的存储引擎

    SESSION_ENGINE = "django.contrib.sessions.backends.cache"
    SESSION_CACHE_ALIAS = "default"
    

    了解
    设置SESSION_ENGINE项指定Session数据存储的方式,可以存储在数据库、缓存、Redis等。

1)存储在数据库中,如下设置可以写,也可以不写,这是默认存储方式。

SESSION_ENGINE='django.contrib.sessions.backends.db'

2)存储在缓存中:存储在本机内存中,如果丢失则不能找回,比数据库的方式读写更快。

SESSION_ENGINE='django.contrib.sessions.backends.cache'

3)混合存储:优先从本机内存中存取,如果没有则从数据库中存取。

SESSION_ENGINE='django.contrib.sessions.backends.cached_db'

猜你喜欢

转载自blog.csdn.net/weixin_43958804/article/details/86292401