有些場景,用戶會頻繁請求 本身的列表類信息,能夠將此類信息放到Redis裏,以訂單列表爲例。redis
/** * @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 + '\'' + '}'; } } }