1.spring cache 基础参数说明

1.1 spring cache 注解说明

1.1.1 @Cacheable

@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果,至于键的话,Spring又支持两种策略,默认策略和自定义策略,这个稍后会进行说明。需要注意的是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。@Cacheable可以指定三个属性,value、key和condition

1.1.1.1 使用key属性自定义key

key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。我们这里先来看看自定义策略,至于默认策略会在后文单独介绍。

自定义策略是指我们可以通过Spring的EL表达式来指定我们的key。这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”

1.1.2 @CachePut

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

@CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的

1.1.3 @CacheEvict

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

1.1.3.1 allEntries属性

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

1.1.3.2 beforeInvocation属性

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

1.1.4 @Caching

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

2.spring cache 中spring EL 使用说明

2.1 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

2.2 spring El语法说明

点击这里供语法参考

https://blog.csdn.net/keda8997110/article/details/52767087

2.开始使用

2.1 依赖

  1. <dependency>
  2. <groupId>com.yjyz</groupId>
  3. <artifactId>inf.common.core</artifactId>
  4. <version>${inf.parent.version}</version>
  5. </dependency>

2.2 配置

#是否关闭redis 打开这个配置则打开redis,否则关闭(默认情况下是关闭状态)
spring.profiles.active=enabledRedis
## Redis 数据库索引 
spring.redis.database=0
##redis ip
spring.redis.host=47.107.175.136
##redis port
spring.redis.port=6379
##超时时间
spring.redis.timeout=10000
##密码 如果redis没有密码该配置可以省略
spring.redis.password=FwOG@Rh1

2.3 使用案列

import com.yjyz.core.annotation.CoreCacheConfig;
import com.yjyz.seed.service.SeedService;
import com.yjyz.seed.vo.Seed;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@CacheConfig(cacheNames = "userCache")
public class SeedServiceImpl implements SeedService{


    /**
     * @CacheEvic @Caching @Cacheable @CachePut 这些注解的用法可以参考spring cache官方文档 ,@CoreCacheConfig是对@CacheConfig封装的一个自定义注解
     *
     *
     * @param seed
     * @return
     */
    @Caching(
            cacheable = {@Cacheable(key = "#seed.id")},
            put = {@CachePut(key = "#seed.id")}
    )
    public Seed seed(Seed seed) {
        seed.setName("hell word !");
        return seed;
    }

    @CacheEvict(key = "#seed.id")
    public void update(Seed seed){
        System.out.println("this is update");
    }

    @CacheEvict(key = "#id")
    public void delete(String id){
        System.out.println("this is delete");
    }

}
@Data
public class Seed implements Serializable {

    private String id;

    private String name;
}
public interface SeedService {

    Seed seed(Seed info);

    void update(Seed seed);

    void delete(String id);

}