引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

修改配置

spring:
  redis:
    host: localhost
    port: 6379
    password:
    database: 0
    jedis:
      pool:
        min-idle: 10
        max-idle: 200
        max-wait: 3000ms
        max-active: 50

注入bean使用

	@Autowired
    private RedisTemplate redisTemplate;
private Map<String, String> getPlatformCodeSites() {
        Map result = new HashMap<>();
        if(redisTemplate.hasKey(ALL_SITES)){
            Object obj = redisTemplate.opsForValue().get(ALL_SITES);
            if(!Ls.isEmpty(obj)){
                result = JSON.parseObject(JSON.toJSONString(obj),Map.class);
                if (result.size() > 0){
                    return result;
                }
            }
        }
        //获取所有平台站点
        IndicatorVO vo = new IndicatorVO();
        vo.setType("CMS_Site");
        Map<String, List<Object>> siteListMap = (Map<String, List<Object>>)
                platformIndicatorSynServiceClient.getIndicatorList(vo);
        if (siteListMap != null && !siteListMap.isEmpty()) {
            for (Map.Entry<String, List<Object>> entry : siteListMap.entrySet()) {
                List<Object> siteMgrSynEOList = entry.getValue();
                String code = entry.getKey();
                if (siteMgrSynEOList != null && !siteMgrSynEOList.isEmpty()) {
                    for (Object obj : siteMgrSynEOList) {
                        SiteMgrSynVO synVO = JSON.parseObject(JSON.toJSONString(obj),SiteMgrSynVO.class);
                        result.put(synVO.getSiteIDCode(),code + "_" + synVO.getIndicatorId());
                    }
                }
            }
            redisTemplate.opsForValue().set(ALL_SITES,JSON.toJSONString(result),600, TimeUnit.SECONDS);
        }
        return result;
    }

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐