Caffine

DBC 1.4K 0

一、Caffine是什么?

Caffeine是一个基于Java 8的[高性能](https://github.com/ben-manes/caffeine/wiki/Benchmarks),[接近最佳的](https://github.com/ben-manes/caffeine/wiki/Efficiency)缓存库

Caffine有什么特色?

    • Count-Min Sketch。基于滑动窗口的时间衰减设计机制,借助于一种简易的reset操作:每次添加一条记录到Sketch的时候,都会给一个计数器上加1,当计数器达到一个尺寸W的时候,把所有记录的Sketch数值都除以2,该reset操作可以起到衰减的作用

二、Caffine缓存的使用

加入依赖

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

简单使用

    com.github.benmanes.caffeine.cache.LoadingCache<Integer,List<TCoupon>> couponCaffeine = Caffeine.newBuilder()
            .expireAfterWrite(10,TimeUnit.MINUTES).refreshAfterWrite(5,TimeUnit.MINUTES)
            .build(new com.github.benmanes.caffeine.cache.CacheLoader<Integer, List<TCoupon>>() {
                @Override
                public List<TCoupon> load(Integer o) throws Exception {
                    return loadCoupon(o);
                }
            });
    /***
     * 获取有效时间的可用优惠券列表
     * // 1、是否存在远程调用 HTTP、RPC Metrics
     * // 2、大量内存处理  list.contain() ==>set.contain
     * @return
     */
    public List<TCoupon> getCouponListCaffeine(){
        List<TCoupon> tCoupons = Lists.newArrayList();
        try {
            tCoupons =  couponCaffeine.get(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tCoupons;
    }

Caffine插图

发表评论 取消回复
表情 图片 链接 代码

分享