zoukankan      html  css  js  c++  java
  • 如何将购物车信息存到Redis中?

    存到Redis中,好处是速度快。毕竟写到硬盘需要更多的时间。加入购物车的功能,操作很频繁,可以通过Redis快速写入,移除,修改。

    用什么方式呢?

    传统的KEY,VALUE不太合适,每次增加修改,都要把VALUE取出,序列化成数组之后,再改变结构,然后序列化存入。

    幸好,Redis中有一种哈希的方式。它的特点就是每个KEY下面,包含对应的子KEY,VALUE。这样就方便操作每个用户下的购物车信息了。

    用户的购物车标识为:

    appid:openid:cart 作为大KEY
    

    购物车中的存储内容为:

    pid:sku_id 作为小KEY  pnum 作为小VALUE
    

    hset 存

    127.0.0.1:6379> hset appid:openid:cart 3:1 10
    (integer) 1
    

    hgetall 取所有

    127.0.0.1:6379> hgetall appid:openid:cart
    1) "1:1"
    2) "1"
    3) "1:2"
    4) "2"
    5) "3:1"
    6) "10"
    

    hkeys 取KEY

    127.0.0.1:6379> hkeys appid:openid:cart
    1) "1:1"
    2) "1:2"
    3) "3:1"
    

    hvals 取值

    127.0.0.1:6379> hvals appid:openid:cart
    1) "1"
    2) "2"
    3) "10"
    

    hdel 删除

    127.0.0.1:6379> hdel appid:openid:cart 3:1
    (integer) 1
    

    hlen 获取长度

    127.0.0.1:6379> hgetall appid:openid:cart
    1) "1:1"
    2) "1"
    3) "1:2"
    4) "2"
    127.0.0.1:6379> hlen appid:openid:cart
    (integer) 2
    

    hset 修改

    127.0.0.1:6379> hset appid:openid:cart 3:1 100
    (integer) 0
    127.0.0.1:6379> hget appid:openid:cart 3:1
    "100"
    

    hincrby 增加,减少

    127.0.0.1:6379> hget appid:openid:cart 3:1
    "100"
    127.0.0.1:6379> hincrby appid:openid:cart 3:1 1
    (integer) 101
    127.0.0.1:6379> hincrby appid:openid:cart 3:1 1
    (integer) 102
    127.0.0.1:6379> hincrby appid:openid:cart 3:1 -1
    (integer) 101
    127.0.0.1:6379> hincrby appid:openid:cart 3:1 -1
    (integer) 100
    127.0.0.1:6379> hincrby appid:openid:cart 3:1 -1
    (integer) 99
    
  • 相关阅读:
    两元素交换(不借用第三个元素)
    魔兽系统
    员工打卡
    NET框架
    jQuery测试
    Android屏幕适配终结者
    android-auto-scroll-view-pager (无限广告轮播图)
    AndroidImageSlider(图片轮播控件)
    PagerSlidingTabStrip(viewPage滑动菜单)
    怎样把淘宝的数据转到拍拍上
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10535974.html
Copyright © 2011-2022 走看看