美文网首页
django缓存

django缓存

作者: 鲸随浪起 | 来源:发表于2019-02-01 01:08 被阅读0次

django-redis官网文档:
http://django-redis-chs.readthedocs.io/zh_CN/latest/
redis.py文档
https://redis-py.readthedocs.io/en/latest/

#Django的缓存配置
#caches缓存,
CACHES={
    "default":{
        "BACKEND":"django_redis.cache.RedisCache",
        "LOCATION":"redis://127.0.0.1:6379/9",
        "OPTIONS":{
            "CLIENT_CLASS":"django_redis.client.DefaultClient",
        }
    }
}
#下面是把用户登录的session存储在redis中
#配置session存储
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

缓存设置

from django.core.cache import cache #缓存
设置缓存:cache.set("yzm",rand_str,20)#键,值,时间
读缓存:cache.get("yzm")#键

redis操作

浏览记录.jpg
#浏览记录
from django_redis import get_redis_connection   #redis数据库
conn = get_redis_connection("default")
history_key = "history_%d"%user.id
#移除列表中的goods_id
conn.lrem(history_key,0,goods_id)
#把goods_id插入列表的左侧
conn.lpush(history_key,goods_id)
#只保存用户最新浏览的5条信息
conn.ltrim(history_key,0,4)
购物车jpg
购物车
        conn = get_redis_connection('default')
        cart_key = "cart_%d" %user.id
        #先尝试获取sku_id的值,使用hget cart_key属性
        #如果sku_id在hash中不存在,hget返回None,hget 键 属性
        cart_count = conn.hget(cart_key,sku_id)
=================================================
        conn = get_redis_connection("default")
        cart_key = "cart_%d"%user.id
        #hgetall:{"商品id":"商品数量"}
        cart_dict = conn.hgetall(cart_key)
        #redis更新
        conn.hset(cart_key,sku_id,count)
        vals = conn.hvals(cart_key) #hvals返回所有的值
        #删除
        conn.hdel(cart_key,sku_id)
        #获取长度
        cart_count = conn.hlen(cart_key)
=================================================
···

相关文章

  • 第十一天

    drf的缓存设置 django本身是支持缓存的,drf的缓存是在django缓存上的二次开发。安装drf exte...

  • django-cache

    以前学习django缓存,视频看的云里雾里,今天读到一篇文章豁然开朗,以此记录。感谢!链接:[Django缓存和信号

  • django项目中使用 memcached (安装与使用)

    在django 中,如果你想提升请求的响应速度,那么使用缓存是很好的办法。django的官网上介绍了如何引入缓存,...

  • 33_Django的缓存

    Django的缓存 全站缓存: 单页面缓存: 页面局部缓存: 缓存的位置:(只需要改配置文件) 内存 文件 数据库...

  • django-settings里redis连接与缓存配置

    Django-redis的缓存配置 配置session存储

  • Django缓存

    为什么使用缓存?以空间换时间。 https://docs.djangoproject.com/zh-hans/2....

  • Django - 缓存

    Python的web框架,就Django支持缓存工具。 根据缓存存放位置可以分为: 一、配置 1.开发调试此为开始...

  • django缓存

    缓存就是将你常用的数据放在内存里面,方便大量的访问,每次请求过来之后,django都会优先访问缓存,看看缓存里面有...

  • django缓存

    django-redis官网文档:http://django-redis-chs.readthedocs.io/z...

  • django 缓存

    连接地址 PythonCourse/web/django at master · RockTeach/Python...

网友评论

      本文标题:django缓存

      本文链接:https://www.haomeiwen.com/subject/oxrrsqtx.html