zoukankan      html  css  js  c++  java
  • 4、redis获取首页购物车商品数目+用户商品历史浏览记录的添加

    一、redis获取首页购物车商品

    利用redis的hash类型保存首页购物车的商品,一个用户对应一个key名cart_(user.id),首页购物车的数量是在用户登陆的情况下才会展示,下面都只是获取redis保存的key值,并还没有设置添加key值的内容:

    #获取用户购物车中商品的数目
    user = request.user
    cart_count = 0
    if user.is_authenticated:#用户已经登陆
        #获取购物车数量使用hash
    
        #连接setting配置的redis
        conn = get_redis_connection('default')
        cart_key = 'cart_%d'%user.id
        #获取物品种类的数量
        cart_count = conn.hlen(cart_key)
    
    1. hash _hlen _获取数量
        def hlen(self, name):
            "Return the number of elements in hash ``name``"
            return self.execute_command('HLEN', name)
    

    二、用户商品历史浏览记录的添加

    利用redis的list类型存储商品浏览的记录,一个用户对应一个key名history_(user.id),下面都只是获取redis保存的key值,并还没有设置添加key值的内容,先删除掉列表里面已存在相同的商品-->把最新浏览的记录添加到列表的左侧,选择保存多少条最新的浏览记录:

    #获取用户购物车中商品的数目
    user = request.user
    cart_count = 0
    if user.is_authenticated:#用户已经登陆
        #获取购物车数量使用hash
    
        #连接setting配置的redis
        conn = get_redis_connection('default')
        cart_key = 'cart_%d'%user.id
        #获取物品种类的数量
        cart_count = conn.hlen(cart_key)
    
        #获取用户历史浏览记录,使用list
        conn = get_redis_connection('default')
        #浏览历史记录每个用户浏览商品的key
        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)
    
    1. list _ lrem (移除,有就移除,没有也就不做任何操作):
        def lrem(self, name, count, value):
            """
            #name=key,
            #count=0删除列表中所有的相同value,count>0删除列表中相同的value从做左到右,count<0删除列表中相同的value从右到左
            
            Remove the first ``count`` occurrences of elements equal to ``value``
            from the list stored at ``name``.
    
            The count argument influences the operation in the following ways:
                count > 0: Remove elements equal to value moving from head to tail.
                count < 0: Remove elements equal to value moving from tail to head.
                count = 0: Remove all elements equal to value.
            """
            return self.execute_command('LREM', name, count, value)
    
    1. list _ lpush (插入)
        #从左往右插入
        def lpush(self, name, *values):
            "Push ``values`` onto the head of the list ``name``"
            return self.execute_command('LPUSH', name, *values)
    
    1. list _ ltrim (相当于切割)
        def ltrim(self, name, start, end):
        
            #name -->key
            #start -->开始的位置
            #end -->结束的位置
            """
            Trim the list ``name``, removing all values not within the slice
            between ``start`` and ``end``
    
            ``start`` and ``end`` can be negative numbers just like
            Python slicing notation
            """
            return self.execute_command('LTRIM', name, start, end)
    
  • 相关阅读:
    C#文件读写常用类介绍
    C#实现注销、重启和关机代码
    Mybatis学习---基础知识考核
    Linux操作系统各版本ISO镜像下载
    Java学习---JDK的安装和配置
    Java学习---基础知识学习
    Java学习---常见的模式
    Java实例---黑白五子棋[单机版]
    Java实例---简单的超市管理系统
    Java实例---简单的个人管理系统
  • 原文地址:https://www.cnblogs.com/venvive/p/12174294.html
Copyright © 2011-2022 走看看