spring cache 1: CacheOperation
CacheOperation 类在spring解析 @Cacheable,@CachePut, @CacheEvict,@CacheConfig 注解中广泛用到
比如如下截图的代码:
会被解析成:
uml
CacheOperation 及其几个子类的继承关系如下:
CacheOperation
CacheOperation 是spring cache操作实现中的一个重要基类。它是对 CacheableOperation,CachePutOperation,CacheEvictOperation的共同部分的抽象。spring cache中的3个注解@Cacheable,@CachePut, @CacheEvict中大部分字段是一样的,而这3个注解中的字段基本上就是CacheOperation及其子类中的字段。
私有字段
/**
* 方法的名称
*/
private final String name;
/**
* 注解@Cacheable,@CachePut,@CacheEvict等中的cacheNames字段
*/
private final Set<String> cacheNames;
/**
* 注解@Cacheable,@CachePut,@CacheEvict等中的key字段
*/
private final String key;
/**
* 注解@Cacheable,@CachePut,@CacheEvict中的keyGenerator字段
*/
private final String keyGenerator;
/**
* 注解@Cacheable,@CachePut,@CacheEvict等中的keyGenerator字段
*/
private final String cacheManager;
/**
* 注解@Cacheable,@CachePut,@CacheEvict等中的cacheResolver字段
*/
private final String cacheResolver;
/**
* 注解@Cacheable,@CachePut,@CacheEvict等中的condition字段
*/
private final String condition;
/**
* toString
*/
private final String toString;
CacheOperation.Builder
Builder 是 CacheOperation 的内部抽象类,同时CacheOperation的3个子类同样有3个特定的Builder内部类继承自CacheOperation.Builder
Builder 类中的私有字段几乎和 CacheOperation 一样,除了没有toString字段
有一个接口 public abstract CacheOperation build(); 需要CacheOperation.Builder的子类实现,目的是用 CacheOperation.Builder构造特定的CacheOperation子类
protected StringBuilder getOperationDescription();
方法用来设置CacheOperation中的toString字段,而这个toString字段描述了当前被@Cacheable,@CachePut或者@CacheEvict中某一个注解修饰的注解中的所有属性信息
/**
* Return an identifying description for this caching operation.
* <p>Available to subclasses, for inclusion in their {@code toString()} result.
*/
protected StringBuilder getOperationDescription() {
StringBuilder result = new StringBuilder(getClass().getSimpleName());
result.append("[").append(this.name);
result.append("] caches=").append(this.cacheNames);
result.append(" | key='").append(this.key);
result.append("' | keyGenerator='").append(this.keyGenerator);
result.append("' | cacheManager='").append(this.cacheManager);
result.append("' | cacheResolver='").append(this.cacheResolver);
result.append("' | condition='").append(this.condition).append("'");
return result;
}
public abstract CacheOperation build();
CacheableOperation
CacheableOperation 继承自 CacheOperation,又比 CacheOperation 多了2个私有字段unless和sync:因为@Cacheable 中可以设置unless和sync,因此 CacheableOperation中就有这两个字段
CacheableOperation.Builder 继承自抽象类 CacheOperation.Builder,同样多了这两个字段,getOperationDescription()方法也重写,把这两个字段拼接上去。
CacheableOperation.Builder 的 build() 方法返回一个 CacheableOperation 实例。
public static class Builder extends CacheOperation.Builder {
// ...
@Override
public CacheableOperation build() {
return new CacheableOperation(this);
}
}
CachePutOperation
CachePutOperation 和 CacheableOperation类似,它继承自 CacheOperation,但比CacheOperation多了一个私有字段unless:因为@CachePut 中可以设置unless,因此 CachePutOperation中就有这个字段
CachePutOperation.Builder 继承自抽象类 CacheOperation.Builder,同样多了这个字段,getOperationDescription()方法也重写,把这个字段拼接上去。
CachePutOperation.Builder 的 build() 方法返回一个 CachePutOperation 实例。
CacheEvictOperation
CacheEvictOperation 也和 CacheableOperation类似,它继承自 CacheOperation,但比CacheOperation多了两个私有字段cacheWide和beforeInvocation:因为@CachePut 中可以设置allEntries和beforeInvocation,因此 CacheEvictOperation 中的cacheWide表示要清除整个cache的数据,beforeInvocation表示在方法调用前清除缓存。虽然这里cacheWide和allEntries不一样,但是含义是一样的。
CacheEvictOperation.Builder 继承自抽象类 CacheOperation.Builder,同样多了这两个字段,getOperationDescription()方法也重写,把这个字段拼接上去。(这里拼的规则有一点点小变化,具体可以看源码,CacheEvictOperation.Builder新加的字段没有用“|”这个符号)
CachePutOperation.Builder 的 build() 方法返回一个 CacheEvictOperation 实例。