加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django-缓存django-redis

发布时间:2020-12-20 10:27:09 所属栏目:Python 来源:网络整理
导读:https://django-redis-chs.readthedocs.io/zh_CN/latest/ 安装 django-redis 最简单的方法就是用 pip : pip install django-redis==4.7.0 ?cache backend 使用配置settings.py # django缓存配置 CACHES = { " default " : { " BACKEND " : " django_redis.ca

https://django-redis-chs.readthedocs.io/zh_CN/latest/

安装 django-redis 最简单的方法就是用 pip :

pip install django-redis==4.7.0

?cache backend 使用配置settings.py

# django缓存配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://127.0.0.1:6379/9","OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",}
    }
}
# 存储在缓存中:存储在本机内存中,如果丢失则不能找回,比数据库的方式读写更快。
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

链接redis数据库,原生客户端使用

在某些情况下你的应用需要进入原生 Redis 客户端使用一些 django cache 接口没有暴露出来的进阶特性. 为了避免储存新的原生连接所产生的另一份设置,django-redis 提供了方法?get_redis_connection(alias)?使你获得可重用的连接字符串.

>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
>>> con
<redis.client.StrictRedis object at 0x2dc4510>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读