Skip to content

LuHanUp/redis-cache-expire-spring-boot-starter-parent

Repository files navigation

redis-cache-expire-spring-boot-starter-parent

English

介绍

当使用redis作为spring cache组件时,使用注解@CacheExpire支持自定义过期时间

  1. 支持方法级别设置过期时间
  2. 支持在类上添加注解,该类下的缓存方法通用过期时间
  3. 支持忽略功能

软件架构

spring-boot-starter-data-redis基础上改变了RedisCacheManagergetCache原始行为 在其基础上添加设置自定义过期时间的功能

使用说明

因为redis-cache-expire-spring-boot-starter并没有集成spring-boot-starter-data-redis所以需要 手动引入依赖

主要是为了方便使用各自的spring-boot-starter-data-redis版本

  1. 引入spring-boot-starter-data-redis依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 引入本starter依赖
<dependency>
    <groupId>top.luhancc.redis</groupId>
    <artifactId>redis-cache-expire-spring-boot-starter</artifactId>
</dependency>

还没有放到maven中央仓库

  1. 和使用spring cache一样 使用@EnableCaching开启缓存即可
@SpringBootApplication
@EnableCaching
public class RedisCacheExpireApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisCacheExpireApplication.class, args);
    }
}
  1. 在需要添加缓存过期时间的方法上添加@CacheExpire注解

示例代码

方法级别

@Service
public class CacheService {
    @Cacheable(value = "cache-test", key = "targetClass + methodName")
    @CacheExpire(value = 100)
    public String data() {
        System.out.println("没走缓存,直接查询");
        return "hello this data is redis cache";
    }
    @Cacheable(value = "cache-test", key = "targetClass + methodName")
    public String data2() {
        System.out.println("没走缓存,直接查询");
        return "hello this data is redis cache is not expire";
    }
}

类级别

@CacheExpire
@Cacheable(value = "cache-test2", key = "targetClass + methodName")
@Service
public class CacheService2 {
    public String data4() {
        return "this is cache data 2";
    }
    @CacheExpire(value = 10, ignore = true)
    public String data3() {
        return "this is cache data 3";
    }
}

参与贡献

  1. Fork 本仓库
  2. 新建 feature/xxx 分支
  3. 提交代码
  4. 新建 Pull Request

About

使用redis作为spring cache组件,并且可以自定义过期时间

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published