Flowable源码注释(二十七)部署缓存类

本文深入探讨Flowable中DeploymentCache接口及其DefaultDeploymentCache实现类,讲解缓存如何在Flowable引擎中用于提升性能。通过查看源码地址,了解详细注释和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flowable源码地址:https://github.com/flowable/flowable-engine

Flowable-6.7.2 源码注释地址:https://github.com/solojin/flowable-6.7.2-annotated

包路径:org.flowable.common.engine.impl.persistence.deploy

DeploymentCache 部署缓存接口类

/**
 * 缓存实现的接口类。
 * 泛型参数
 * 
 * @author Joram Barrez
 */
public interface DeploymentCache<T> {

    // 根据ID获取缓存实体
    T get(String id);

    // 根据ID判断是否包含
    boolean contains(String id);

    // 根据ID新的缓存实体
    void add(String id, T object);

    // 根据ID移除缓存实体
    void remove(String id);

    // 清除全部缓存
    void clear();

    // 获取所有缓存实体
    Collection<T> getAll();

    // 获取缓存实体的数量
    int size();
}

DefaultDeploymentCache 默认部署缓存实现类

/**
 * 默认缓存:将所有内容保留在内存中,除非设置了限制。
 * 用Map结构存储缓存数据
 *
 * @author Joram Barrez
 */
public class DefaultDeploymentCache<T> implements DeploymentCache<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDeploymentCache.class);

    protected Map<String, T> cache;

    /** 不做限制的缓存
     * 译者注:Collections.synchronizedMap的同步Map方法效率比较低,因为内部使用的是Synchronized锁,推荐使用JUC包下的ConcurrentHashMap实现
     */
    public DefaultDeploymentCache() {
        this.cache = Collections.synchronizedMap(new HashMap<>());
    }

    /**
     * 具有硬限制的缓存:缓存的元素不会超过参数int limit的限制。
     */
    public DefaultDeploymentCache(final int limit) {
        // 译者注:Collections.synchronizedMap的同步Map方法效率比较低,因为内部使用的是Synchronized锁,推荐使用JUC包下的ConcurrentHashMap实现
        this.cache = Collections.synchronizedMap(new LinkedHashMap<String, T>(limit + 1, 0.75f, true) { // +1 is needed, because the entry is inserted first, before it is removed
            // 加载因子默认值为0.75(参见javadocs)
            // true将保留“访问顺序”,这是实现LRU缓存算法(即最近最少使用页面置换算法)所需的字段
            private static final long serialVersionUID = 1L;

            @Override
            protected boolean removeEldestEntry(Map.Entry<String, T> eldest) {
                boolean removeEldest = size() > limit;
                if (removeEldest && LOGGER.isTraceEnabled()) {
                    // 已达到缓存限制,{}将被逐出
                    LOGGER.trace("Cache limit is reached, {} will be evicted", eldest.getKey());
                }
                return removeEldest;
            }

        });
    }

    @Override
    public T get(String id) {
        return cache.get(id);
    }

    @Override
    public void add(String id, T obj) {
        cache.put(id, obj);
    }

    @Override
    public void remove(String id) {
        cache.remove(id);
    }

    @Override
    public boolean contains(String id) {
        return cache.containsKey(id);
    }

    @Override
    public void clear() {
        cache.clear();
    }

    @Override
    public Collection<T> getAll() {
        return cache.values();
    }

    @Override
    public int size() {
        return cache.size();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值