package com.qianwen.mdc.collect.cache; import cn.hutool.core.date.DateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.qianwen.mdc.collect.dto.CacheBuildDTO; import com.qianwen.mdc.collect.dto.CalendarShiftTimeSlicesDTO; import com.qianwen.mdc.collect.dto.TimestampToProductionTimeCacheDto; import com.qianwen.mdc.collect.service.CalendarService; import com.qianwen.mdc.collect.utils.CommonUtil; import com.qianwen.mdc.collect.utils.redis.RedisUtil; @Component public class TimeSliceCache { public static final String COLLECT_CALENDAR = "collect:calendar"; public static final String CALENDAR = "calendar"; public static final String CALENDARS_CODE = "calendar:code:"; public static final String MINUTE_POINT = "minute-point:"; private static final Logger log = LoggerFactory.getLogger(TimeSliceCache.class); @Autowired private RedisUtil redisUtil; @Autowired private CalendarService calendarService; //@Autowired //private BladeRedis bladeRedis; public CalendarShiftTimeSlicesDTO getTimeSliceShift(String calendarCode, Date date) { int minutes = (DateTime.of(date).hour(true) * 60) + DateTime.of(date).minute();//日期转化为分钟数,作为hash key从redis取切片数据数据 String redisKey = CommonUtil.getReallyCacheName(COLLECT_CALENDAR, CALENDARS_CODE.concat(calendarCode).concat(":").concat(MINUTE_POINT), DateTime.of(date).toDateStr()); return (CalendarShiftTimeSlicesDTO)redisUtil.getRedisTemplate().opsForHash().get(redisKey, Integer.valueOf(minutes)); } //构建并保存时间切片,保存到redis public void build(CacheBuildDTO cacheBuildDTO) { String key = cacheBuildDTO.getTargetDate().format(DateTimeFormatter.ISO_LOCAL_DATE); List data = calendarService.buildProductionTimeCache(cacheBuildDTO);//查询生产日历,并计算出时间切片 data.forEach(t -> { String redisKey = CommonUtil.getReallyCacheName(COLLECT_CALENDAR, CALENDARS_CODE.concat(t.getCalendarCode()).concat(":").concat(MINUTE_POINT), key); Map timeSlicesDtoMap = t.getTimeSlicesDTOMap(); //redisUtil.hmset(redisKey, timeSlicesDtoMap, 259200L); redisUtil.getRedisTemplate().opsForHash().putAll(redisKey, timeSlicesDtoMap); }); } }