caffeine缓存

  1. 配置
@Configuration
@EnableCaching
public class CacheConfigration {

    private static final int INITIAL_CAPACITY = 1000;

    private static final int MAX_IMUM_SIZE = 10000;

    @Bean("caffeineCacheManager")
    @Primary
    public CacheManager caffeineCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<CaffeineCache> caffeineCaches = new ArrayList<>();

        for (CacheTypeEnum cacheType : CacheTypeEnum.values()) {
            caffeineCaches.add(new CaffeineCache(cacheType.name(),
                    Caffeine.newBuilder()
                            .initialCapacity(INITIAL_CAPACITY) //初始大小
                            .maximumSize(MAX_IMUM_SIZE) //最大大小
                            .expireAfterWrite(cacheType.getExpires(), cacheType.getUnit())
                            .build()));
        }
        cacheManager.setCaches(caffeineCaches);
        return cacheManager;
    }

    @Bean
    public CacheLoader<String, Object> cacheLoader() {
        return new CacheLoader<String, Object>() {
            @Override
            public Object load(String key) throws Exception {
                return null;
            }

            // 重写这个方法将oldValue值返回回去,进而刷新缓存
            @Override
            public Object reload(String key, Object oldValue) throws Exception {
                return oldValue;
            }
        };
    }
}

枚举值

public enum CacheTypeEnum {
    // 缓存90天
    NINETY_DAYS(90, TimeUnit.DAYS),
    // 缓存30天
    THIRTY_DAYS(30, TimeUnit.DAYS),
    // 30分钟
    THIRTY_MINUTES(30, TimeUnit.MINUTES),
    //缓存1小时
    ONE_HOURS(1, TimeUnit.HOURS),
    //缓存2小时
    TWO_HOURS(2, TimeUnit.HOURS),
    //缓存1分钟
    ONE_MINUTES(1, TimeUnit.MINUTES),
    //缓存10分钟
    TEN_MINUTES(10, TimeUnit.MINUTES),
    //6个小时
    SIX_HOURS(6, TimeUnit.HOURS);

    private int expires;
    private TimeUnit unit;

    CacheTypeEnum(int expires, TimeUnit unit) {
        this.expires = expires;
        this.unit = unit;
    }

    public int getExpires() {
        return expires;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public static String getCacheName(int expires, TimeUnit unit) {
        if (null == unit) {
            Asserts.fail("缓存时间单位不能为空");
        }
        for (CacheTypeEnum enums : values()) {
            if (expires == enums.getExpires() && unit.name().equals(enums.getUnit().name())) {
                return enums.name();
            }
        }
        Asserts.fail("未配置对应缓存");
        return null;
    }


  1. 启动类添加@EnableCaching注解
  2. 工具类
@Component
public class CacheUtils<K, V> {
    @Resource
    private CacheManager caffeineCacheManager;

    /**
     * 设置指定时间内过期的key
     *
     * @param key
     * @param value
     */
    public void setEx(K key, V value, String cacheName) {
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if (null == cache) {
            Asserts.fail("未配置缓存信息");
        }
        cache.put(key, value);
    }

    /**
     * 根据key获取相应的值
     *
     * @param key
     * @return
     */
    public V get(K key, String cacheName) {
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if (null == cache) {
            Asserts.fail("未配置缓存信息");
        }
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (null == valueWrapper) {
            return null;
        }
        return (V) valueWrapper.get();
    }

    /**
     * 移除指定的key
     *
     * @param key
     */
    public void remove(K key, String cacheName) {
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if (null == cache) {
            Asserts.fail("未配置缓存信息");
        }
        cache.evict(key);
    }
}
  1. 使用方法
1. 注解: @Cacheable(cacheNames = "枚举名", key = RedisKeyConst.AREA_TREE_KEY + "+#blockNoStationTownship")
2. 工具类
 cacheUtils.setEx(RedisKeyConst.DICTIONARY_ID_KEY + dict.getCode() + CommonSymbolConstant.UNDERLINE + dict.getValue(),
                    dict.getDictDesc(), CacheTypeEnum.THIRTY_DAYS.name());

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值