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 %}
  • 相关阅读:
    POJ 1328 Radar Installation
    POJ 1700 Crossing River
    POJ 1700 Crossing River
    poj 3253 Fence Repair (贪心,优先队列)
    poj 3253 Fence Repair (贪心,优先队列)
    poj 3069 Saruman's Army(贪心)
    poj 3069 Saruman's Army(贪心)
    Redis 笔记与总结2 String 类型和 Hash 类型
    数据分析方法有哪些_数据分析方法
    数据分析方法有哪些_数据分析方法
  • 原文地址:https://www.cnblogs.com/yifengs/p/11614854.html
Copyright © 2011-2022 走看看