zoukankan      html  css  js  c++  java
  • django-缓存django-redis

    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>

     视图函数views.py中

     # 获取历史记录
            from django_redis import get_redis_connection
            con = get_redis_connection('default')  # defaultsettings.py中cache设置中
            history_key = 'history_%d'%user.id
            # 获取用户最新浏览的5个商品的id
            sku_ids = con.lrange(history_key, 0, 4)
            # 从数据库中查询用户浏览的商品的具体信息
            from goods.models import GoodsSKU
            # 方法1
            # goods_ids = GoodsSKU.objects.get(id__in=sku_ids)
            # goods_list = []
            # for s_id in sku_ids:
            #     for goods in goods_ids:
            #         if s_id == goods.id:
            #             goods_list.append(goods)
            # 方法2
            goods_li = []
            # 根据redis取出的列表的id顺序查询出商品 遍历出来
            for id in sku_ids:
                goods = GoodsSKU.objects.get(id=id)
                goods_li.append(goods)
    
            # 组织上下文
            context = {
                'address': address,
                'page': 'user',
                'goods_li':goods_li
            }

    模板中

      {% for goods in goods_li %}
                    <li>
                        {# 路由带参数#}
                        <a href="{% url 'goods:detail' %}"><img src="{{ goods.image.url }}"></a>
                        <h4><a href="detail.html">{{ goods.name }}</a></h4>
                        <div class="operate">
                            <span class="prize">¥{{ goods.price }}</span>
                            <span class="unit">{{ goods.price }}/{{ goods.unite }}</span>
                            <a href="#" class="add_goods" title="加入购物车"></a>
                        </div>
                    </li>
                    {% empty %}
                    <li>暂无浏览记录</li>
                    {% endfor %}
  • 相关阅读:
    如何设置范围,使透视数据源记录可以自适应地改变
    Atitit..文件上传组件选择and最佳实践的总结(2)----HTTP
    AIDL(1)
    最好的年龄减肥
    2012在数据库技术会议上的讲话PPT打包
    左右 Java 于 finally 深度分析语句块
    R0-R37它是Arm 寄存器,那是,CPU内部。和GPIO注册所有外设。换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO寄存器只有一个特定的芯片。
    使用方便 正则表达式grep,sed,awk(一)
    经验36--C#无名(大事,物...)
    IOS 图片压缩
  • 原文地址:https://www.cnblogs.com/yifengs/p/11614854.html
Copyright © 2011-2022 走看看