Spring缓存注解

@CacheConfig

用于配置该类中会用到的一些共用的缓存配置。主要属性:


  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package org.springframework.cache.annotation;

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
public @interface CacheConfig {

//缓存存储的集合名
    java.lang.String[] cacheNames() default {};
//指定key生成器
    java.lang.String keyGenerator() default "";
//指定使用哪个缓存管理器,当有多个时才需要使用
    java.lang.String cacheManager() default "";
//指定使用哪个缓存解析器
    java.lang.String cacheResolver() default "";
}

@Cacheable 

声明方法是可缓存的。将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方法。直接从缓存中取值。主要属性:


  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package org.springframework.cache.annotation;

@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Inherited
@java.lang.annotation.Documented
public @interface Cacheable {
//用于指定缓存存储的集合名。cacheNames的别名
    @org.springframework.core.annotation.AliasFor("cacheNames")
    java.lang.String[] value() default {};
//用于指定缓存存储的集合名。value的别名
    @org.springframework.core.annotation.AliasFor("value")
    java.lang.String[] cacheNames() default {};
//缓存对象存储在Map集合中的key值
//默认key的生成按照以下规则: 
//- 如果没有参数,则使用0作为key 
//- 如果只有一个参数,使用该参数作为key 
//- 如果又多个参数,使用包含所有参数的hashCode作为key
    java.lang.String key() default "";
//用于指定key生成器
    java.lang.String keyGenerator() default "";
//指定使用哪个缓存管理器
    java.lang.String cacheManager() default "";
//指定使用那个缓存解析器
    java.lang.String cacheResolver() default "";
//缓存对象的条件
    java.lang.String condition() default "";
//另一个缓存条件参数,该条件是在函数被调用之后才做判断的
    java.lang.String unless() default "";
//指示底层将缓存锁住,使只有一个线程可以进入计算,而其他线程堵塞,直到返回结果更新到缓存中。 
    boolean sync() default false;
}

@CacheEvict 

不仅支持将数据缓存,还支持将缓存数据删除。通常用在删除方法上,用来从缓存中移除相应数据。


  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package org.springframework.cache.annotation;

@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Inherited
@java.lang.annotation.Documented
public @interface CacheEvict {
    @org.springframework.core.annotation.AliasFor("cacheNames")
    java.lang.String[] value() default {};

    @org.springframework.core.annotation.AliasFor("value")
    java.lang.String[] cacheNames() default {};

    java.lang.String key() default "";

    java.lang.String keyGenerator() default "";

    java.lang.String cacheManager() default "";

    java.lang.String cacheResolver() default "";

    java.lang.String condition() default "";
//是否需要清除缓存中的所有元素。
    boolean allEntries() default false;
//默认会在调用方法之后移除数据。当为true时,会在调用方法之前移除数据。
    boolean beforeInvocation() default false;
}

 @CachePut

@CachePut标注的方法每次都会执行,并将执行结果以键值对的形式存入指定的缓存中。

@EnableCaching

启用缓存,用在启动java类上。


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