springboot 集成redis 并设置失效时间

该文详细介绍了如何在SpringBoot应用中配置Redis作为缓存,包括在配置文件中设置Redis连接信息,启用缓存注解@EnableCaching,以及在控制器方法上使用@Cacheable定义缓存逻辑。同时,文章还展示了如何自定义缓存过期时间和检查过期时间的设置。
摘要由CSDN通过智能技术生成

1、配置文件加入配置

    type: redis  (用redis缓存)
    cache-names: wasData (保存分组名称叫wasData )


spring:
  cache:
    type: redis
    cache-names: wasData
  redis:
    host: 127.0.0.1
    port: 6379
    pool:
      max-idle: 100
      min-idle: 1
      max-active: 1000
      max-wait: -1

2、在启动类加上注解@EnableCaching


@SpringBootApplication
@EnableCaching
public class ProjectStartApplication extends SpringBootServletInitializer{
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ProjectStartApplication.class);
		app.run(args);
	}
}

3、控制类的接口上加上注解@Cacheable()

cacheNames = "wasData"  分组名称叫wasData,与配置文件对应

key = "#type != null ? #type : 'defaultKey'"  保存的key使用我的方法入参String type,写法为 @Cacheable(cacheNames = "分组名", key = #入参")

#type != null ? #type : 'defaultKey' 如果我的key没有传或者等null,使用默认生成的key

unless = "#result == null" 如果我的返回值为null,不进行缓存

    @ResponseBody
    @DZ_AuthPassport
    @RequestMapping(value = "getWasData")
    @Cacheable(cacheNames = "wasData", key = "#type != null ? #type : 'defaultKey'", unless = "#result == null")
    public String getWasData(String type) {
        JSONObject json = new JSONObject();
        if (StringUtil.isBlankOrNull(type)) {
            json.put("error", true);
            return null;
        }
        json.put("type", type);
        String url = "";
        switch (type) {
            case "ttlist": {
                url = wasPrefix.getTtlist();
                break;
            }
            case "xwdetail": {
                url = wasPrefix.getXwdetail();
                break;
            }
        }

        if (StringUtil.isBlankOrNull(url)) {
            return null;
        }
        String result = HttpClientUtils.sendHttpGet(url);
        json.put("error", false);
        json.put("data", result);
        return json.toJSONString();
    }

以上已经可以实现保存,测试接口

redis当中没有缓存时,断点到接口中是可以进入接口方法的。当第一次执行之后,生成缓存,第二次执行接口,没有进入方法,直接从缓存取数据返回

 

 设置过期时间

package com.terton.api.config;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class CustomRedisCacheManager {
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //默认使用cacheNames作为key的前缀
        rcm.setUsePrefix(true);
        //设置缓存过期时间
        Map<String, Long> expires = new HashMap<>();
        expires.put("wasData", 30L);
//        expires.put("1h", 3600 * 1L);
//        expires.put("10m", 60 * 10L);
        rcm.setExpires(expires);
//        rcm.setDefaultExpiration(60 * 60 * 12);//默认过期时间
        return rcm;
    }
}

查看过期时间是否设置成功,如果没过期时间,值为-1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值