多级缓存架构开发 二 ( 使用Caffeine作为一级缓存 )

1.导入caffeine的jar包

     在父模块的pom文件中添加

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>com.github.ben-manes.caffeine</groupId>
                <artifactId>caffeine</artifactId>
                <version>${caffeine.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

  在子模块的pom文件中添加

    <dependencies>

        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

    </dependencies>

2.开发一级缓存具体的实现类

package org.github.roger.cache.caffeine;

import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.github.roger.cache.AbstractValueAdaptingCache;
import org.github.roger.enumeration.ExpireMode;
import org.github.roger.settings.FirstCacheSetting;
import org.github.roger.support.NullValue;

import java.util.concurrent.Callable;

@Slf4j
public class CaffeineCache extends AbstractValueAdaptingCache {

    /** caffeine 缓存对象 */
    private Cache<Object,Object> cache;


    /**
     * 使用name和{@link FirstCacheSetting}创建一个 {@link CaffeineCache} 实例
     *
     * @param name              缓存名称
     * @param firstCacheSetting 一级缓存配置 {@link FirstCacheSetting}
     */
    public CaffeineCache(String name, FirstCacheSetting firstCacheSetting) {
        super( name);
        this.cache = getCache(firstCacheSetting);
    }

    /**
     *  构建一个caffeine缓存对象,用于存储一级缓存
     * @param firstCacheSetting 一级缓存配置
     * @return  一级缓存对象
     */
    private Cache<Object,Object> getCache(FirstCacheSetting firstCacheSetting) {
        //根据一级缓存设置,构建caffeine缓存对象
        Caffeine<Object,Object> cacheBuilder = Caffeine.newBuilder();
        cacheBuilder.initialCapacity(firstCacheSetting.getInitialCapacity());
        cacheBuilder.maximumSize(firstCacheSetting.getMaximumSize());
        if(ExpireMode.WRITE.equals(firstCacheSetting.getExpireMode())){
            cacheBuilder.expireAfterWrite(firstCacheSetting.getExpireTime(),firstCacheSetting.getTimeUnit());
        }
        if(ExpireMode.ACCESS.equals(firstCacheSetting.getExpireMode())){
            cacheBuilder.expireAfterAccess(firstCacheSetting.getExpireTime(),firstCacheSetting.getTimeUnit());
        }
        return cacheBuilder.build();
    }


    public boolean isAllowNullValues() {
        return false;
    }

    public Object getRealCache() {
        return this.cache;
    }

    public Object get(Object key) {
        log.debug("caffeine缓存 key={} 获取缓存", JSON.toJSONString(key));

        if (this.cache instanceof LoadingCache) {
            return ((LoadingCache<Object, Object>) this.cache).get(key);
        }

        return cache.getIfPresent(key);
    }

    public <T> T get(Object key, Callable<T> valueLoader) {
        log.debug("caffeine缓存 key={} 获取缓存, 如果没有命中就走库加载缓存", JSON.toJSONString(key));
       // 获取key对应的缓存值,如果没有,就使用valuLoader 获取值,类似设置一个默认值
        Object result = this.cache.get(key, (k) -> loaderValue(key, valueLoader));
        // 如果不允许存NULL值 直接删除NULL值缓存
        boolean isEvict = !isAllowNullValues() && (result == null || result instanceof NullValue);
        if (isEvict) {
            evict(key);
        }
        return (T) fromStoreValue(result);
    }

    /**
     * 加载数据
     */
    private <T> Object loaderValue(Object key, Callable<T> valueLoader) {
        try {
            T t = valueLoader.call();
            log.debug("caffeine缓存 key={} 从库加载缓存", JSON.toJSONString(key), JSON.toJSONString(t));

            return toStoreValue(t);
        } catch (Exception e) {
            throw new LoaderCacheValueException(key, e);
        }
    }

    public void put(Object key, Object value) {
        // 允许存NULL值
        if (isAllowNullValues()) {
            log.debug("caffeine缓存 key={} put缓存,缓存值:{}", JSON.toJSONString(key), JSON.toJSONString(value));
            this.cache.put(key, toStoreValue(value));
            return;
        }

        // 不允许存NULL值
        if (value != null && !(value instanceof NullValue)) {
            log.debug("caffeine缓存 key={} put缓存,缓存值:{}", JSON.toJSONString(key), JSON.toJSONString(value));
            this.cache.put(key, toStoreValue(value));
        }
        log.debug("缓存值为NULL并且不允许存NULL值,不缓存数据");
    }

    public Object putIfAbsent(Object key, Object value) {
        log.debug("caffeine缓存 key={} putIfAbsent 缓存,缓存值:{}", JSON.toJSONString(key), JSON.toJSONString(value));
        boolean flag = !isAllowNullValues() && (value == null || value instanceof NullValue);
        if (flag) {
            return null;
        }
        Object result = this.cache.get(key, k -> toStoreValue(value));
        return fromStoreValue(result);
    }

    public void evict(Object key) {
        log.debug("caffeine缓存 key={} 清除缓存", JSON.toJSONString(key));
        this.cache.invalidate(key);
    }

    public void clear() {
        log.debug("caffeine缓存 key={} 清空缓存");
        this.cache.invalidateAll();
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值