从Redis读取数据推送到前端

controller层,返回给前端 list数组形式,如果单独返回对象,把list去掉就好

    //定时
//    private static final Logger log = LoggerFactory.getLogger(StationChargerRedisIni.class);
	
	/**
     * 定时更新数据
     */
    @ApiOperation("仓库信息")
    @GetMapping("/Redis")
//    @Scheduled(cron = "0 */1 * * * ?")
    public List<CabinetDto> Charger()  throws InstantiationException, IllegalAccessException, IOException {
//        log.info(".........定时开始.........");

        List<CabinetDto> cabinetDtoList = geelyChargingOrderService.show();
        System.out.println("CabinetDto:+++++++++++++++++++++" + cabinetDtoList);

//        log.info(".........定时结束.........");
        return cabinetDtoList;
    }

serviceimpl层,我的类是嵌套
list1{a :1,list2{c:1,list3{e:1}}}

redisTemplate.opsForValue().get()里面可以直接写key

    @Override
    public List<CabinetDto> show() {
        //new仓库表,查找仓库所在的字段
        GeelyConfigItemParam geelyConfigItemParam = new GeelyConfigItemParam();
        geelyConfigItemParam.setParamCode("CABINET_SN_LIST");
        //查询仓库所在的位置
        List<GeelyConfigItemParam> geelyConfigItemParamList = geelyConfigItemParamMapper.selectGeelyConfigItemParamList(geelyConfigItemParam);
        if (geelyConfigItemParamList == null && geelyConfigItemParamList.size()==0){
            new RuntimeException("仓库不存在");
        }
        GeelyConfigItemParam geelyConfigItemParam1 = geelyConfigItemParamList.get(0);
        String s = geelyConfigItemParam1.getParamValue();
        ArrayList<String> list = new ArrayList<>();
        Object station1 = null;
        String[] strings = null;

        Object station = redisTemplate.opsForValue().get(RedisConstants.getStationChargerKey(RedisConstants.Station_Charger));
        System.out.println(station);
        List<CabinetDto> cabinetDtoList = new ArrayList<>();
        if (s !=null && s.length() != 0){
            try {
                strings = s.split(",");
                for (int i = 0,len = strings.length; i < len; i++) {
                    list.add(strings[i]);
                    station1 = redisTemplate.opsForValue().get(RedisConstants.getStationChargerKey(strings[i]));
                    if (station1 != null) {
                        ChargerDto chargerDto = JSON.parseObject(station1.toString(), ChargerDto.class);
                        CabinetDto cabinetDto = new CabinetDto();
                        cabinetDto.setCabinetNo(chargerDto.getChargerNo());
                        cabinetDto.setCharger(chargerDto);
                        cabinetDtoList.add(cabinetDto);
                        System.out.println("station1 = " + station1);
                    }else if (cabinetDtoList == null) {
                        cabinetDtoList = new ArrayList<CabinetDto>();
                        List<GeelyChargingOrder> geelyStationInfos = geelyChargingOrderMapper.selectGeelyChargingOrderList(null);
                        if (geelyStationInfos != null && geelyStationInfos.size() == 1) {
                            geelyStationInfos.get(0);
                            redisTemplate.opsForValue().set(RedisConstants.getStationChargerKey(RedisConstants.Station_Charger), JSONObject.toJSONString(cabinetDtoList));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            new RuntimeException("仓库不存在");
        }
        System.out.println(list.toString()+"aaaaaaaaaaaaaaaaaaaaaa");
        return cabinetDtoList;
    }

redisTemplate.opsForValue().get(RedisConstants.getStationChargerKey(RedisConstants.Station_Charger));
可以创建类,把key写在一起,方便查看

package com.geely.hc1y.common.constant;

/**
 * ClassName RedisKey
 * Description
 * Create by Bingnan.Wang
 * Date 2021/9/25 9:16
 */
public class RedisConstants {
	
    public static String getStationChargerKey(String key) {
    	//Redis的key是station:Charger:5
        return "station:Charger:".concat(key);
    }

    /**
     * 仓位
     */
    public static final String Station_Charger = "5";
}


版权声明:本文为qq_48012160原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。