Redis場景用法1、列表類(訂單列表)

  • 1、使用場景介紹

    有些場景,用戶會頻繁請求 本身的列表類信息,能夠將此類信息放到Redis裏,以訂單列表爲例。redis

  • 2、功能原理分析
  1. 訂單詳情用hash結構存儲。Redis 裏的 hash 是key--value(Map)形式,這中形式能夠充當二維表結構。以hash的方式存儲對象數據,相對於String的key-value數據能夠更加集約的利用內存結構。讀取的時候,能夠經過訂單ID直接獲得Map,解析封裝成對象。

    如圖所示:

     

  2. 訂單列表用List結構存儲。Redis裏的List結構是以key-value(ArrayList)形式,這種隊列方式,能夠靈活的PUSH和POP,根據業務靈活的掌握,先進先出,仍是先進後出。舉個列子 好比說:訂單以時間順序排序(時間由近及遠),那麼就訂單生成後 能夠用LPUSH的方式將訂單插入LIST。調用列表的時候, 直接從左到右(LRANGE,0,-1)排序調出全部列表。

    如圖所示:

     

  • 3、demo實例

    

/**
 * @Auther: DBG_zxx
 * @Date: 2019/1/2 18:34
 * @Desc: Redis訂單列表的DEMO
 */
public class OrderList {

    final static String KEY_ORDER_ID = "orderId_";
    final static String KEY_ORDER_LIST = "order_list";

    final static JedisUtils jedis = new JedisUtils(Basic.ip, Basic.port, Basic.auth);

    // 批量插入 訂單
    public static void batchAddOrderDetails(List<Map<String,String>> orders) {
        orders.forEach(order ->{
            if (StringUtils.isNotBlank(order + "")){
                if (StringUtils.isNotBlank(order.get("orderId") + "")){
                    String redisOrderId = KEY_ORDER_ID + order.get("orderId").toString();
                    jedis.hmset(redisOrderId ,order);
                    if ("OK".equals(jedis.hmset(redisOrderId ,order))){
                        System.out.println("插入訂單ID爲 : " +redisOrderId + "成功");
                    }
                    lpushOrderId(redisOrderId);
                }
            }
        });
        System.out.println("-----------------------------------------------------");
    }

    public static Long lpushOrderId(String order){
        return jedis.lpush(KEY_ORDER_LIST , order);
    }

    public static void orderList(String orderListKey){
        List<String> orderList = jedis.lrange(orderListKey , 0 , -1);
        System.out.println("訂單列表爲:");
        orderList.forEach(orderId -> {
            System.out.println(orderId);
        });
        System.out.println("-----------------------------------------------------");
    }

    public static void orderDetails(String orderId){
        Map<String ,String > orderDetails = jedis.hgetall(orderId);
        System.out.println("訂單詳情:");
        orderDetails.forEach((k,v) ->System.out.println(k+" : "+v));
    }

    public static void main(String[] args) {
        List<Map<String ,String >> orderList = new ArrayList<>();
        orderList.add(obj2Map(new Order("1", "36.6" , "2018-01-01")));
        orderList.add(obj2Map(new Order("2", "38.6" , "2018-01-01")));
        orderList.add(obj2Map(new Order("3", "39.6" , "2018-01-01")));
        // 批量插入訂單
        batchAddOrderDetails(orderList);
        // 查詢訂單列表
        orderList(KEY_ORDER_LIST);
        // 根據訂單Id查詢訂單詳情
        orderDetails(KEY_ORDER_ID+1);

    }

    public static Map<String ,String > obj2Map(Order order){
        Map<String ,String > map = new HashMap<>();
        map.put("orderId",order.getOrderId());
        map.put("money",order.getMoney());
        map.put("money",order.getTime());
        return map;
    }

    static class Order{

        private String orderId;
        private String money;
        private String time;

        public Order(String orderId, String money, String time) {
            this.orderId = orderId;
            this.money = money;
            this.time = time;
        }

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        @Override
        public String toString() {
            return "Order{" +
                    "orderId='" + orderId + '\'' +
                    ", money='" + money + '\'' +
                    ", time='" + time + '\'' +
                    '}';
        }
    }

}
相關文章
相關標籤/搜索