Guava Cache缓存入门

一、在什么场景下需要使用缓存呢?

缓存在很多场景下都是需要使用的。比如在需要一个值的过程和代价特别高的情况下,而且对这个值的需要不止一次的情况下,我们可能就需要考虑使用缓存了。

二、在什么场景下需要使用本地缓存呢?

一般来说要使用本地缓存,首先,是缓存中的数据总量不会超过内存的容量。并且你愿意消耗一些内存来提升速度。

三、那怎么实现本地缓存呢?

一般来说我们可以直接使用jdk里提供的数据结构来作为缓存,但这样有个问题就是缓存的一些机制,比如缓存过期的淘汰策略,缓存的初始化,缓存最大容量的设置,缓存的共享等等一些列的问题需要自己去考虑和实现。
第二种方法就是我们可以使用一些业界开源的,成熟的一些第三方的工具来帮助我们实现缓存。这其中有:EHCache,cahce4j等等好多框架和工具。但从我使用的来看我认为google里guava包内的缓存工具是我使用过的最方便,简单的缓存框架。
下面就来介绍这个Guava包内的CacheBuilder。

四、Guava Cache工作方式

GuavaCache的工作流程:获取数据->如果存在,返回数据->计算获取数据->存储返回。由于特定的工作流程,使用者必须在创建Cache或者获取数据时指定不存在数据时应当怎么获取数据。GuavaCache采用LRU的工作原理,什么是LRU呢?
这里引入一个概念,缓存命中率:从缓存中获取到数据的次数/全部查询次数,命中率越高说明这个缓存的效率好。由于机器内存的限制,缓存一般只能占据有限的内存大小,缓存需要不定期的删除一部分数据,从而保证不会占据大量内存导致机器崩溃。
如何提高命中率呢?那就得从删除一部分数据着手了。目前有三种删除数据的方式,分别是:FIFO(先进先出)、LFU(定期淘汰最少使用次数)、LRU(淘汰最长时间未被使用)。GuavaCache采用LRU的工作原理,
使用者必须指定缓存数据的大小,当超过缓存大小时,必定引发数据删除。GuavaCache还可以让用户指定缓存数据的过期时间,刷新时间等等很多有用的功能。

五、Guava Cache的Demo演示

在Guava Cache中其实可以创建两种类型的本地缓存,分别是类型为LoadingCache和Cache的本地缓存。
首先定义一个存储的bean,如下:

package com.xujin.guava;

/**
 * @author xujin
 * @createtime 2020-07-05 15:44
 * @description
 */

public class Human {
    private String idCard;
    private String name;

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Human{" +
                "idCard=" + idCard +
                ", name='" + name + '\'' +
                '}';
    }
}

类型为LoadingCache的缓存:

package com.xujin.guava;

import com.google.common.cache.*;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author xujin
 * @createtime 2020-07-05 15:39
 * @description
 */

public class TestLoadingCache {
    private LoadingCache<String, Human> loadingCache;
    private RemovalListener<String, Human> removalListener;

    @Before
    public void InitLoadingCache() {
        //移除key-value的监听器
        //可以使用RemovalListeners.asynchronous方法将移除监听器设为异步方法
        //removalListener = RemovalListeners.asynchronous(removalListener, new ThreadPoolExecutor(1,1,1000, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(1)));
        removalListener = new RemovalListener<String, Human>() {
            public void onRemoval(RemovalNotification<String, Human> notification) {
                Logger logger = LoggerFactory.getLogger("RemovalListener");
                //可以在监听器中获取key,value,和删除原因
                //EXPLICIT、REPLACED、COLLECTED、EXPIRED、SIZE
                logger.info("key" + notification.getKey() + ",value" + notification.getValue() + "被移除,原因:" + notification.getCause());
            }
        };
        //指定一个如果数据不存在获取数据的方法
        CacheLoader<String, Human> cacheLoader = new CacheLoader<String, Human>() {
            @Override
            public Human load(String key) throws Exception {
                //模拟mysql操作
                Logger logger = LoggerFactory.getLogger("LoadingCache");
                logger.info("LoadingCache测试 从mysql加载缓存ing...(2s)");
                Thread.sleep(2000);
                logger.info("LoadingCache测试 从mysql加载缓存成功");
                Human human = new Human();
                human.setIdCard(key);
                human.setName("其他人");
                if (key.equals("1")) {
                    human.setName("张三");
                    return human;
                }
                if (key.equals("2")) {
                    human.setName("李四");
                    return human;
                }
                return human;
            }
        };
        loadingCache = CacheBuilder.newBuilder().
                //当缓存项在指定的时间段内没有被读或写就会被回收。
                        expireAfterAccess(2, TimeUnit.MINUTES).
                //当缓存项在指定的时间段内没有更新就会被回收。
                        expireAfterWrite(2, TimeUnit.MINUTES).
                //当缓存项距上一次更新操作之后的多久,下一次查询操作会异步的刷新缓存
                        refreshAfterWrite(1, TimeUnit.MINUTES).
                //设置key为弱引用
                        weakKeys().
                //最大的缓存数量为1,为了展示缓存删除效果
                        maximumSize(1).
                        removalListener(removalListener).
                        build(cacheLoader);
    }

    //获取数据,如果不存在返回null
    public Human getIfPresentLoadingCache(String key) {
        return loadingCache.getIfPresent(key);
    }

    //获取数据,如果数据不存在则通过cacheLoader获取数据,缓存并返回
    public Human getCacheKeyLoadingCache(String key) {
        try {
            return loadingCache.get(key);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    //直接向缓存put数据
    public void putLoadingCache(String key, Human value) {
        Logger logger = LoggerFactory.getLogger("LoadingCache");
        logger.info("put key :{} value : {}", key, value);
        loadingCache.put(key, value);
    }

    @Test
    public void test01() {
        System.out.println("使用loadingCache get方法  第一次加载");
        Human human = getCacheKeyLoadingCache("1");
        System.out.println(human);

        System.out.println("\n使用loadingCache getIfPresent方法  第一次加载");
        human = getIfPresentLoadingCache("2");
        System.out.println(human);

        System.out.println("\n使用loadingCache get方法  第一次加载");
        human = getCacheKeyLoadingCache("2");
        System.out.println(human);

        System.out.println("\n使用loadingCache get方法  已加载过");
        human = getCacheKeyLoadingCache("2");
        System.out.println(human);

        System.out.println("\n使用loadingCache get方法  已加载过,但是已经被剔除掉,验证重新加载");
        human = getCacheKeyLoadingCache("1");
        System.out.println(human);

        System.out.println("\n使用loadingCache getIfPresent方法  已加载过");
        human = getIfPresentLoadingCache("1");
        System.out.println(human);

        System.out.println("\n使用loadingCache put方法  再次get");
        Human newHuMan = new Human();
        newHuMan.setIdCard("1");
        newHuMan.setName("额外添加");
        putLoadingCache("1", newHuMan);
        human = getCacheKeyLoadingCache("1");
        System.out.println(human);
    }
}

运行结果如下图:

使用loadingCache get方法  第一次加载
20:10:16.877 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存ing...(2s)
20:10:18.888 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存成功
Human{idCard=1, name='张三'}

使用loadingCache getIfPresent方法  第一次加载
null

使用loadingCache get方法  第一次加载
20:10:18.888 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存ing...(2s)
20:10:20.899 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存成功
20:10:20.899 [main] INFO RemovalListener - key1,valueHuman{idCard=1, name='张三'}被移除,原因:SIZE
Human{idCard=2, name='李四'}

使用loadingCache get方法  已加载过
Human{idCard=2, name='李四'}

使用loadingCache get方法  已加载过,但是已经被剔除掉,验证重新加载
20:10:20.899 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存ing...(2s)
20:10:22.918 [main] INFO LoadingCache - LoadingCache测试 从mysql加载缓存成功
20:10:22.918 [main] INFO RemovalListener - key2,valueHuman{idCard=2, name='李四'}被移除,原因:SIZE
Human{idCard=1, name='张三'}

使用loadingCache getIfPresent方法  已加载过
Human{idCard=1, name='张三'}


使用loadingCache put方法  再次get
20:10:22.918 [main] INFO LoadingCache - put key :1 value : Human{idCard=1, name='额外添加'}
20:10:22.918 [main] INFO RemovalListener - key1,valueHuman{idCard=1, name='张三'}被移除,原因:REPLACED
Human{idCard=1, name='额外添加'}

类型为Cache的缓存:

package com.xujin.guava;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author xujin
 * @createtime 2020-07-05 16:53
 * @description
 */

public class TestCache {
    private Cache<String, Human> cache;
    private RemovalListener<String, Human> removalListener;

    @Before
    public void InitDefault() {
        //移除key-value的监听器
        removalListener = new RemovalListener<String, Human>() {
            public void onRemoval(RemovalNotification<String, Human> notification) {
                Logger logger = LoggerFactory.getLogger("RemovalListener");
                //可以在监听器中获取key,value,和删除原因
                //EXPLICIT、REPLACED、COLLECTED、EXPIRED、SIZE
                logger.info("key" + notification.getKey() + ",value" + notification.getValue() + "被移除,原因:" + notification.getCause());
            }
        };
        cache = CacheBuilder.newBuilder().
                expireAfterAccess(2, TimeUnit.MINUTES).
                expireAfterWrite(2, TimeUnit.MINUTES).
                //refreshAfterWrite(1,TimeUnit.MINUTES).//没有设置cacheLoader这个参数的缓存不能设置刷新,因为没有指定获取数据的方式
                        weakKeys().
                        maximumSize(1).
                        removalListener(removalListener).
                        build();
    }

    public Human getIfPresentCache(String key) {
        return cache.getIfPresent(key);
    }

    public Human getCacheKeyCache(final String key) {
        try {
            return cache.get(key, new Callable<Human>() {
                public Human call() throws Exception {
                    //模拟mysql操作
                    Logger logger = LoggerFactory.getLogger("Cache");
                    logger.info("Cache测试 从mysql加载缓存ing...(2s)");
                    Thread.sleep(2000);
                    logger.info("Cache测试 从mysql加载缓存成功");
                    Human human = new Human();
                    human.setIdCard(key);
                    human.setName("其他人");
                    if (key.equals("1")) {
                        human.setName("张三");
                        return human;
                    }
                    if (key.equals("2")) {
                        human.setName("李四");
                        return human;
                    }
                    return human;
                }
            });
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void putCache(String key, Human value) {
        Logger logger = LoggerFactory.getLogger("Cache");
        logger.info("put key :{} value : {}", key, value);
        cache.put(key, value);
    }

    @Test
    public void test01() {
        System.out.println("使用Cache get方法  第一次加载");
        Human human = getCacheKeyCache("1");
        System.out.println(human);

        System.out.println("\n使用Cache getIfPresent方法  第一次加载");
        human = getIfPresentCache("2");
        System.out.println(human);

        System.out.println("\n使用Cache get方法  第一次加载");
        human = getCacheKeyCache("2");
        System.out.println(human);

        System.out.println("\n使用Cache get方法  已加载过");
        human = getCacheKeyCache("2");
        System.out.println(human);

        System.out.println("\n使用Cache get方法  已加载过,但是已经被剔除掉,验证重新加载");
        human = getCacheKeyCache("1");
        System.out.println(human);

        System.out.println("\n使用Cache getIfPresent方法  已加载过");
        human = getIfPresentCache("1");
        System.out.println(human);

        System.out.println("\n使用Cache put方法  再次get");
        Human newHuman = new Human();
        newHuman.setIdCard("1");
        newHuman.setName("额外添加");
        putCache("1", human);
        newHuman = getCacheKeyCache("1");
        System.out.println(newHuman);
    }
}

运行结果如下:


使用Cache get方法  第一次加载
20:20:14.311 [main] INFO Cache - Cache测试 从mysql加载缓存ing...(2s)
20:20:16.311 [main] INFO Cache - Cache测试 从mysql加载缓存成功
Human{idCard=1, name='张三'}

使用Cache getIfPresent方法  第一次加载
null

使用Cache get方法  第一次加载
20:20:16.311 [main] INFO Cache - Cache测试 从mysql加载缓存ing...(2s)
20:20:18.321 [main] INFO Cache - Cache测试 从mysql加载缓存成功
20:20:18.321 [main] INFO RemovalListener - key1,valueHuman{idCard=1, name='张三'}被移除,原因:SIZE
Human{idCard=2, name='李四'}


使用Cache get方法  已加载过
Human{idCard=2, name='李四'}


使用Cache get方法  已加载过,但是已经被剔除掉,验证重新加载
20:20:18.321 [main] INFO Cache - Cache测试 从mysql加载缓存ing...(2s)
Disconnected from the target VM, address: '127.0.0.1:55503', transport: 'socket'
20:20:20.341 [main] INFO Cache - Cache测试 从mysql加载缓存成功
20:20:20.341 [main] INFO RemovalListener - key2,valueHuman{idCard=2, name='李四'}被移除,原因:SIZE
Human{idCard=1, name='张三'}

使用Cache getIfPresent方法  已加载过
Human{idCard=1, name='张三'}

由上述结果可以表明,Guava Cache可以在数据存储到达指定大小后删除数据结构中的数据。我们可以设置定期删除而达到定期从数据库、磁盘等其他地方更新数据等(再次访问时数据不存在重新获取)。类型为LoadingCache 也可以采用定时刷新的方式更新数据。还可以设置移除监听器对被删除的数据进行一些操作。通过RemovalListeners.asynchronous(RemovalListener,Executor)方法将监听器设为异步,笔者通过实验发现,异步监听不会在删除数据时立刻调用监听器方法。

六、深入Guava Cache的refresh和expire刷新机制

首先看一下三种基于时间的清理或刷新缓存数据的方式:
expireAfterAccess: 当缓存项在指定的时间段内没有被读或写就会被回收。

expireAfterWrite:当缓存项在指定的时间段内没有更新就会被回收。

refreshAfterWrite:当缓存项距上一次更新操作之后的多久,下一次查询操作会异步的刷新缓存

考虑到时效性,我们可以使用expireAfterWrite,使每次更新之后的指定时间让缓存失效,然后重新加载缓存。guava cache会严格限制只有1个加载操作,这样会很好地防止缓存失效的瞬间大量请求穿透到后端引起雪崩效应。

然而,guava cache在限制只有1个加载操作时进行加锁,其他请求必须阻塞等待这个加载操作完成;而且,在加载完成之后,其他请求的线程会逐一获得锁,去判断是否已被加载完成,每个线程必须轮流地走一个“”获得锁,获得值,释放锁“”的过程,这样性能会有一些损耗。这里由于我们计划本地缓存1秒,所以频繁的过期和加载,锁等待等过程会让性能有较大的损耗。

因此我们考虑使用refreshAfterWrite。refreshAfterWrite的特点是,在refresh的过程中,严格限制只有1个重新加载操作,而其他查询先返回旧值,这样有效地可以减少等待和锁争用,所以refreshAfterWrite会比expireAfterWrite性能好。但是它也有一个缺点,因为到达指定时间后,它不能严格保证所有的查询都获取到新值。guava cache并没使用额外的线程去做定时清理和加载的功能,而是依赖于查询请求。在查询的时候去比对上次更新的时间,如超过指定时间则进行加载或刷新。所以,如果使用refreshAfterWrite,在吞吐量很低的情况下,如很长一段时间内没有查询之后,发生的查询有可能会得到一个旧值(这个旧值可能来自于很长时间之前),这将会引发问题。

可以看出refreshAfterWrite和expireAfterWrite两种方式各有优缺点,各有使用场景。那么能否在refreshAfterWrite和expireAfterWrite找到一个折中?比如说控制缓存每1s进行refresh,如果超过2s没有访问,那么则让缓存失效,下次访问时不会得到旧值,而是必须得待新值加载,答案是可以的。

七、基于引用的回收

在说这一小节之前,我们先了解以下什么是Java中的强引用,软引用,弱引用和虚引用,大家可以参照这篇博客
通过弱引用的键或者弱引用的值,或者软引用的值,guava Cache可以把缓存设置为允许垃圾回收
**CacheBuilder.weakKeys():**使用过弱引用存储键值。当被垃圾回收的时候,当前键值没有其他引用的时候缓存项可以被垃圾回收。

**CacheBuilder.weakValues():**使用弱引用存储值。

**CacheBuilder.softValues():**使用软引用存储值。软引用就是在内存不够是才会按照顺序回收。

八、GuavaCache结构初探

从前面的介绍我们可以知道GuavaCache分为两种Cache:Cache,LoadingCache。LoadingCache继承了Cache,他比Cache主要多了get和refresh方法。
从源码上我们可以知道在创建Cache类型的本地缓存的时候,其实创建的是LocalManualCache这个类的实例,如下图:
在这里插入图片描述
而这个LocalManualCache又实现了Cache接口,在这个类的内部定义了本地缓存,localCache就是本地缓存,如下图:
在这里插入图片描述
而在创建LoadingCache类型的本地缓存的时候,其实创建的是LocalLoadingCache这个类的实例,
在这里插入图片描述
而这个类继承与LocalManualCache并实现LoadingCache,如下图:
在这里插入图片描述
那么本地缓存的结构是怎么样的呢?其实结构和ConcurrentHashMap差不多,都是数组+链表实现的。首先,把整个本地缓存分成N个段,如下图:
在这里插入图片描述
而每个段都是一个特殊的HashTable,不是说Segment是HashTable的子类,而是结构上类似,内部维护这一个类似ArrayList的类用于存储Entry,如下图:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值