方法hset(String key,String field,String value),hmset(String key, Map<String,String> hash),hgetAll()
hash结构(key value)
业务场景:当信物无感时间超过一定时间 就再次出现
实现:
用redis 存储无感信物去exclude:
//1.批量获取用户的无感信物列表:
(判断无感信物有效期是否超过):
Map<String, String> userGiftList = giftCacheFacade.getUser(userId);
(内置方法:Map<String, String> hash = redisClientTemplate.hgetAll(key))
Map<String, String> newUserGiftList = Maps.newHashMap();
//用户的无感信物id列表
List<Integer> excludeGiftIds = Lists.newArrayList();
for (String giftIdStr : userGiftList.keySet()) {
String giftExpireTime = userGiftList.get(giftIdStr);
if (System.currentTimeMillis() < Long.valueOf(giftExpireTime)) {
newUserGiftList.put(giftIdStr, giftExpireTime);
excludeGiftIds.add(Integer.valueOf(giftIdStr));
}
}
//2.更新缓存
newUserGiftList.forEach((k, v) -> giftCacheFacade.setUser(userId, Integer.parseInt(k), Long.parseLong(v)));
(内置方法:redisClientTemplate.hset(userId, giftId + "", expireTime + ""))