django-redis配置
阿新 • • 發佈:2019-01-13
django-redis配置
將session儲存到redis資料庫
django預設將session資訊儲存到mysql資料庫,如果使用者訪問量增大,會給mysql資料庫服務造成非常大的壓力。
redis快取資料庫,將資料儲存到記憶體,存取速度快。
django
參考文件:http://django-redis-chs.readthedocs.io/zh_CN/latest/#
- 安裝
pip install django-redis - 作為 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
-
配置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'