-------------------14-1-----14-2----14-3---------------------------------------------------------
redis安装。
------------------------14-4----------------------
zadd:注意是分数放在前面,值放在后面
zrange:索引分为
zrangebyscore:指定分数范围
扫描二维码关注公众号,回复:
9921231 查看本文章
zrem:移除值
zrank:值的排名
------------------------14-5----------------------
如何引用redis:
第一步:配置yml文件
第二步:配置config序列化
第三步:些测试方法
------------------------14-6----------------------
公告栏:
------------------------14-7----------------------
数据展示:
数据维护:
public List<String> queryCouponList(){
Set<String> couponSet = redisTemplate.opsForZSet().reverseRange(COUPON,0,-1);
//获取set里面前N条数据
return couponSet.stream().limit(COUPON_NUM).collect(Collectors.toList());
}
------------------------14-8----------------------
/**
* 接收coupon优惠券核销mq的时候被调用,以时间窗口展示前N条数据,userCouponStr代表userId_couponId
* 拿掉时间最早的
*/
public void updateCoupon(String userCouponStr){
redisTemplate.opsForZSet().add(COUPON,userCouponStr,System.currentTimeMillis());
Set<String> couponSet = redisTemplate.opsForZSet().range(COUPON,0,-1);
if(couponSet.size()>COUPON_NUM){
String remUserCouponStr = couponSet.stream().findFirst().get();
redisTemplate.opsForZSet().remove(COUPON,remUserCouponStr);
}
}
------------------------14-9------------------------
dubbo接口。
/**
* 查询coupon公告栏,前10条数据 userId_couponId ==> 1_1==> string[] string[0] =1,string[1] =1
*/
@Override
public List<CouponNoticeDto> queryCouponNotice(){
Set<String> couponSet = redisTemplate.opsForZSet().reverseRange(COUPON,0,-1);
//获取set里面前N条数据
List<String> userCouponStrs = couponSet.stream().limit(COUPON_NUM).collect(Collectors.toList());
Map<String,String> couponUserMap =userCouponStrs.stream().collect(Collectors.toMap(o -> o.split("_")[1],o -> o.split("_")[0]));
List<String> couponIds = userCouponStrs.stream().map(s -> s.split("_")[1]).collect(Collectors.toList());
//[1,2] ==>1,2 StringUtils.join
String couponIdStrs = StringUtils.join(couponIds,",");
//通过couponIdStrs批量获取coupon缓存数据
List<TCoupon> tCoupons = getCouponListByIds(couponIdStrs);
List<CouponNoticeDto> dtos = tCoupons.stream().map(tCoupon -> {
CouponNoticeDto dto = new CouponNoticeDto();
BeanUtils.copyProperties(tCoupon,dto);
dto.setUserId(Integer.parseInt(couponUserMap.get(dto.getId()+"")));
return dto;
}).collect(Collectors.toList());
return dtos;
}
------------------------14-10----------------------
debug:
@Test
public void queryCouponNotice() {
String ret = JSON.toJSONString(couponService.queryCouponNotice());
System.err.println(ret);
} //本地maven ==》 nexus
private List<News> queryNotice() {
List<CouponNoticeDto> dtos = iCouponService.queryCouponNotice();
List<News> newsList = dtos.stream().map(dto -> {
News news = new News();
int userId = dto.getUserId();
int reduce = dto.getReduceAmount();
String title = dto.getTitle();
String title1 = "恭喜" + userId + "使用" + title + "优惠券,获得减免金额" + reduce;
news.setTitle(title1);
news.setCreateTime(new Date());
return news;
}).collect(Collectors.toList());
return newsList;
}
------------------------14-11----------------------