RedisTemplate操作hash数据类型示例

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 定时任务
     */
    @Transactional
    public void generateDispatcherPlanBak() {
        String hashKey ="DISPATCHER_GENERATE_KEY:PLAN";
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 10; i++) {
            String d = f.format(DateUtils.addDays(new Date(), i));//2020-01-14
            List dispatchers = driverDispatcherDao.getDispatchers(new DriverDispatcherVO(dateToWeek(d, f)));//星期二
            if (!CollectionUtils.isEmpty(dispatchers)) {
                // dispatchers.stream().distinct();
                dispatchers.forEach(dispatcher -> {
                    //判断当前线路当前日期是否已经生成过出班,已经生成过的直接跳过,没有生成的根据排班表来生成
                    StringBuilder sb = new StringBuilder();
                    String key = sb.append(dispatcher.getLineId().toString())
                                    .append(d)
                                    .append(dispatcher.getStartTime())
                                    .append(dispatcher.getEndTime())
                                    .toString();
                    String value = redisTemplate.opsForHash().get(hashKey,key).toString();
                    if (value==null){
                        Driver driver = driverService.getDriverForRedisById(dispatcher.getDriverId());
                        if (driver!=null && driver.getState()== DriverStateEnum.NORMAL) {
                            DispatcherPlanVO planVO = new DispatcherPlanVO();
                            BeanUtils.copyProperties(dispatcher, planVO);
                            planVO.setDay(d);
                            setStartAndEndDateTime(planVO);
                            planVO.setDriverName(driver.getName());
                            planVO.setDriverPhone(driver.getTelephone());
                            planVO.setVehicleNo(driver.getCar().getVehicleNo());
                            planVO.setVehicleBrand(driver.getCar().getBrand());
                            planVO.setVehicleColor(driver.getCar().getVehicleColor());
                            planVO.setVehicleModel(driver.getCar().getVehicleModel());
                            planVO.setSeats(Integer.parseInt(driver.getCar().getSeats()==null?"0":driver.getCar().getSeats()));
                            planVO.setCarTypeId(driver.getCarTypeId());
                            driverDispatcherDao.saveDispatcherPlan(planVO);
                        }
                    }
                });
                dispatchers.forEach(dispatcher->{
                    StringBuilder sb = new StringBuilder();
                    String key = sb.append(dispatcher.getLineId().toString())
                            .append(d)
                            .append(dispatcher.getStartTime())
                            .append(dispatcher.getEndTime())
                            .toString();
                    redisTemplate.opsForHash().put(hashKey,key,key);
                });
            }
        }
    }

 

你可能感兴趣的:(Redis)