Complete e-commerce project--(6) Commodity module (8): User browsing history

User browsing history introduction

  • We record up to five browsing history product records of users, and present
  • Use redis as storage

Redis data type selection (document)

  • demand analysis
    • We need to save five browsing records for each user, and we need to know who the user is
  • Data type selection:
    • Implemented using list type
    • Storage format : history_user_id': [sku_id_1, sku_id_2, …]

redis storage logic

  • First need to remove the duplication
    • Join already stored [1,2] , insert 2 again at this time, it cannot be [2,1, 2], so use deduplication
  • Save
  • Intercept list:
    • There are only five data, which need to be intercepted each time, and the part exceeding the length of 5 is not needed
  • Pipeline execution to improve performance efficiency
 # 保存用户浏览数据
        redis_conn = get_redis_connection('history')
        pl = redis_conn.pipeline()
        user_id = request.user.id

        # 先去重, 0 表示去除所有的相同元素
        pl.lrem('history_%s' % user_id, 0, sku_id)
        # 再存储
        pl.lpush('history_%s' % user_id, sku_id)
        # 最后截取
        pl.ltrim('history_%s' % user_id, 0, 4)
        # 执行管道
        pl.execute()

that's it. You only need to execute the query when you need it
Insert picture description here

Read data command

Insert picture description here

Very simple, it is to use redis to store browsing records.

Guess you like

Origin blog.csdn.net/pythonstrat/article/details/108174286