redis存储用户历史浏览记录

半城伤御伤魂 提交于 2019-11-27 08:32:24

1:什么时候需要添加浏览记录

访问商品详情页的时候

2:什么时候取出浏览记录

访问用户个人中心的时候

 

用户查看商品的历史浏览记录存成什么格式:

history_用户id:[商品id,商品id]

添加用户浏览记录:

# 添加用户的历史记录history_用户id:[商品id,商品id,商品id]conn = get_redis_connection('default')history_key = 'history_%d' % user.id# 用redis的lrem方法将最早的那一条浏览记录移除掉conn.lrem(history_key, 0, goods_id)#   把goods_id插入到列表的左侧conn.lpush(history_key, goods_id)#   只保存用户最新浏览的5条信息conn.ltrim(history_key, 0, 4)取出浏览记录:
# 获取用户的历史浏览记录# 获取redis链接conn = get_redis_connection('default')history_key = 'history_%d' % user.id# 获取用户最新浏览的5个商品的idsku_ids = conn.lrange(history_key, 0, 4)  # [2,5,1,6,8]# 从数据库查询用户浏览的商品的具体信息# goods_list = GoodsSKU.objects.filter(id__in=sku_ids)# 遍历获取用户浏览的历史商品信息goods_li = []for id in sku_ids:    goods = GoodsSKU.objects.get(id=id)    goods_li.append(goods)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!