Spring Cache基础组件 BasicOperation

相关阅读

简介

所有缓存动作需实现的接口,定义了获取缓存标识符集合的方法;

核心代码

/**
 * 获取缓存标识符集合
 */
Set<String> getCacheNames();

实现子类

public interface BasicOperation
    public abstract class CacheOperation implements BasicOperation
        public class CacheableOperation extends CacheOperation
        public class CacheEvictOperation extends CacheOperation
        public class CachePutOperation extends CacheOperation

CacheOperation

简介

缓存动作的基础抽象类,构造器模式构造CacheOperation实例;

核心代码

// 名称
private final String name;
// 缓存标识符集合
private final Set<String> cacheNames;
// 缓存KEY
private final String key;
// 缓存KEY生成器
private final String keyGenerator;
// 缓存管理器
private final String cacheManager;
// 缓存解析器
private final String cacheResolver;
// 先决条件
private final String condition;
// toString值
private final String toString;


/**
 * 获取缓存标识符集合
 */
public Set<String> getCacheNames() {
     return this.cacheNames;
}

/**
 * 构造方法
 * 使用构造器构造
 */
protected CacheOperation(Builder b) {
     this.name = b.name;
     this.cacheNames = b.cacheNames;
     this.key = b.key;
     this.keyGenerator = b.keyGenerator;
     this.cacheManager = b.cacheManager;
     this.cacheResolver = b.cacheResolver;
     this.condition = b.condition;
     this.toString = b.getOperationDescription().toString();
}

/**
 * 比较方法
 */
@Override
public boolean equals(@Nullable Object other) {
     return (other instanceof CacheOperation && toString().equals(other.toString()));
}

/**
 * hashCode方法
 */
@Override
public int hashCode() {
     return toString().hashCode();
}

/**
 * toString方法
 * 模板方法,子类不可重写
 */
@Override
public final String toString() {
     return this.toString;
}


/**
 * 提供构造CacheOperation的实现
 * 内部类
 */
public abstract static class Builder {

    // 名称
    private String name = "";
    // 缓存标识符集合
    private Set<String> cacheNames = Collections.emptySet();
    // 缓存KEY
    private String key = "";
    // 缓存KEY生成器
    private String keyGenerator = "";
    // 缓存管理器
    private String cacheManager = "";
    // 缓存解析器
    private String cacheResolver = "";
    // 先决条件
    private String condition = "";


    /**
     * 获取动作描述
     */
    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;
    }

    /**
     * 构造CacheOperation
     * 由子类实现算法细节
     */
    public abstract CacheOperation build();
}

CacheableOperation

简介

缓存添加动作的具体实现;

核心代码

// 后决条件
@Nullable
private final String unless;
// 同步标识
private final boolean sync;


/**
 * 构造方法
 */
public CacheableOperation(CacheableOperation.Builder b) {
     super(b);
     this.unless = b.unless;
     this.sync = b.sync;
}


/**
 * 提供构造CacheableOperation的实现
 * 内部类,继承于CacheOperation.Builder
 */
public static class Builder extends CacheOperation.Builder {

    // 后决条件
    @Nullable
    private String unless;
    // 同步标识
    private boolean sync;


    /**
     * 获取动作描述
     */
    @Override
    protected StringBuilder getOperationDescription() {
        StringBuilder sb = super.getOperationDescription();
        sb.append(" | unless='");
        sb.append(this.unless);
        sb.append('\'');
        sb.append(" | sync='");
        sb.append(this.sync);
        sb.append('\'');
        return sb;
    }

    /**
     * 构造CacheableOperation
     */
    @Override
    public CacheableOperation build() {
        return new CacheableOperation(this);
    }
}

CacheEvictOperation

简介

缓存清除动作的具体实现;

核心代码

// 全部清除标识
private final boolean cacheWide;
// 在方法执行前执行标识
private final boolean beforeInvocation;


/**
 * 构造方法
 */
public CacheEvictOperation(CacheEvictOperation.Builder b) {
     super(b);
     this.cacheWide = b.cacheWide;
     this.beforeInvocation = b.beforeInvocation;
}


/**
 * 提供构造CacheEvictOperation的实现
 * 内部类,继承于CacheOperation.Builder
 */
public static class Builder extends CacheOperation.Builder {

    // 全部清除标识
    private final boolean cacheWide;
    // 在方法执行前执行标识
    private final boolean beforeInvocation;


    /**
     * 获取动作描述
     */
    @Override
    protected StringBuilder getOperationDescription() {
         StringBuilder sb = super.getOperationDescription();
         sb.append(',');
         sb.append(this.cacheWide);
         sb.append(',');
         sb.append(this.beforeInvocation);
         return sb;
    }

    /**
     * 构造CacheEvictOperation
     */
    @Override
    public CacheEvictOperation build() {
         return new CacheEvictOperation(this);
    }
}

CachePutOperation

简介

缓存更新动作的具体实现;

核心代码

// 后决条件
@Nullable
private final String unless;


/**
 * 构造方法
 */
public CachePutOperation(CachePutOperation.Builder b) {
     super(b);
     this.unless = b.unless;
}


/**
 * 提供构造CachePutOperation的实现
 * 内部类,继承于CacheOperation.Builder
 */
public static class Builder extends CacheOperation.Builder {

    // 后决条件
    private final String unless;


    /**
     * 获取动作描述
     */
    @Override
    protected StringBuilder getOperationDescription() {
        StringBuilder sb = super.getOperationDescription();
        sb.append(" | unless='");
        sb.append(this.unless);
        sb.append('\'');
        return sb;
    }

    /**
     * 构造CachePutOperation
     */
    @Override
    public CachePutOperation build() {
         return new CachePutOperation(this);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值