Spring boot 自带注解缓存

常用缓存注解

CacheConfig Cacheable CachePut CacheEvict Caching
当然我们也可以自定义缓存注解
注意:spring自带的缓存功能,实质上是通过java类来保存缓存的数据,这样会占用一定的内存消耗,并发率越高,对内存的压力越大。

CacheConfig

一个类中可能会有多个缓存操作,而这些缓存操作可能是重复的。这个时候可以使用@CacheConfig。@CacheConfig是一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager 和CacheResolver。 该操作会被覆盖。此三者都可以自定义。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheConfig {

	String[] cacheNames() default {};

	String keyGenerator() default "";  //键生成器

	String cacheManager() default ""; //Spring的中央缓存管理器SPI。允许检索命名的Cache区域。

	String cacheResolver() default "";//确定要用于截获方法调用的Cache实例。实现必须是线程安全的。

}

Cacheable

public @interface Cacheable {

	@AliasFor("cacheNames")
	String[] value() default {};

	@AliasFor("value")
	String[] cacheNames() default {};

	String key() default "";

	String keyGenerator() default "";

	String cacheManager() default "";

	String cacheResolver() default "";

	String condition() default "";

	String unless() default "";
	
	//如果多个线程试图加载同一个键的值,则同步底层方法的调用。同步会导致一些限制:
		不支持unless,
		只能指定一个缓存,
		不能合并其他与缓存相关的操作
	boolean sync() default false;

}

显然,value和cacheNames等价
key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。

@Cacheable(value=”users”, key=”#id”)
public User find(Integer id) {
   return null;
}
 
@Cacheable(value=”users”, key=”#p0”)
public User find(Integer id) {
    return null;
}
 
@Cacheable(value=”users”, key=”#user.id”)
public User find(User user) {
    return null;
}
 
@Cacheable(value=”users”, key=”#p0.id”)
public User find(User user) {
    return null;
}
 

除了上述使用方法参数作为key之外,Spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。

属性名称描述示例
methodName当前方法名#root.methodName
method当前方法#root.method.name
target当前被调用的对象#root.target
targetClass当前被调用的对象的class#root.targetClass
args当前方法参数组成的数组#root.args[0]
caches当前被调用的方法使用的Cache#root.caches[0].name

当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为Spring默认使用的就是root对象的属性。如:

@Cacheable(value={“users”, “xxx”}, key=”caches[1].name”)
public User find(User user) {
      return null;
}

KeyGenerator:指定密钥生成器:https://blog.csdn.net/king101125s/article/details/104479175

condition:指定缓存发生的条件

有的时候我们可能并不希望缓存一个方法所有的返回结果。通过condition属性可以实现这一功能。condition属性默认为空,表示将缓存所有的调用情形。其值是通过SpringEL表达式来指定的,当为true时表示进行缓存处理;当为false时表示不进行缓存处理,即每次调用该方法时该方法都会执行一次。如下示例表示只有当user的id为偶数时才会进行缓存。

@Cacheable(value={“users”}, key=”#user.id”, condition=”#user.id%2==0”)
 
   public User find(User user) {
 
      System.out.println(“find user by user “ + user);
 
      return user;
 
   }

unless

与condition相反

sync

如果多个线程试图加载同一个键的值,则同步底层方法的调用。同步会导致一些限制
1.unless不支持
2.仅仅缓存被指定的
3.不能合并其他与缓存相关的操作

@CachePut

缓存更新
在支持Spring Cache的环境下,对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。@CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的。

@CacheEvict

@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与@Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的(对应Cache的名称);key表示需要清除的是哪个key,如未指定则会使用默认策略生成的key;condition表示清除操作发生的条件。下面我们来介绍一下新出现的两个属性allEntries和beforeInvocation。

allEntries

allEntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率。

beforeInvocation

清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间,当我们指定该属性值为true时,Spring会在调用该方法之前清除缓存中的指定元素。

@Caching

@Caching注解可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。

@Caching(
			cacheable={
				@Cacheable(cacheNames = {"user"},key="#id")
			},
			put={
				@CachePut(value="user",key="#result.id"),
				@CachePut(value="user",key="#result.name")
			},
			evict={
				@CacheEvict(value="emp",key="#id")
			}
			)
	public void caching() {
		
	}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值