Guava Cache学习笔记

文章目录

  • 官网api doc
  • 部分重要方法
  • demo
  • 源码
  • 总结
  • 参考文章

官网api doc

点我鸭.

再点com.google.common.cache里面这个

部分重要方法

CacheBuilder

方法 作用
maximumSize Specifies the maximum number of entries the cache may contain. Note that the cache may evict an entry before this limit is exceeded. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn’t been used recently or very often.
expireAfterWrite 当写完缓存一定时间内没有操作,将过期回收
refreshAfterWrite 当写完后将更新
removalListener 移除侦听器,移除之后做相关操作
build 重写CacheLoader方法,load(),reload(),loadAll()
load() :加载或者过期之后重新加载的方法
reload():重新加载之后执行其他操作
loadAll():加载所有key

expireAfterWrite 与 refreshAfterWrite区别

CacheBuilder.refreshAfterWrite(long, TimeUnit)可以为缓存增加自动定时刷新功能。和expireAfterWrite相反,refreshAfterWrite通过定时刷新可以让缓存项保持可用,但请注意:缓存项只有在被检索时才会真正刷新(如果CacheLoader.refresh实现为异步,那么检索不会被刷新拖慢)。因此,如果你在缓存上同时声明expireAfterWrite和refreshAfterWrite,缓存并不会因为刷新盲目地定时重置,如果缓存项没有被检索,那刷新就不会真的发生,缓存项在过期时间后也变得可以回收。

demo

package com.example.demo;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

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

/**
 * Created on 2019/12/2.
 *
 * @author dajitui
 */
public class CacheTest {

    private static String aa = "";

    public static void main(String[] args) throws InterruptedException {

        LoadingCache graphs = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(1, TimeUnit.SECONDS)
                .refreshAfterWrite(1, TimeUnit.SECONDS)
                .build(
                        new CacheLoader() {
                            @Override
                            public String load(String key) {
                                return a(key);
                            }
                        });
        aa = "123";

        try {
            System.out.println(graphs.get("123"));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        aa = "666";
        Thread.sleep(2000);
        try {
            System.out.println(graphs.get("123"));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        Thread.sleep(2000);
        try {
            System.out.println(graphs.get("123"));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println();
    }

    public static String a(String key) {
        return aa;
    }

}

自己改改过期时间试一下,当没有过期的时候会返回旧的数据,当过期的时候会重新查询。

源码

graphs.get(“123”)—>
Guava Cache学习笔记_第1张图片
在这里插入图片描述
Guava Cache学习笔记_第2张图片
真正重头戏来了
Guava Cache学习笔记_第3张图片
我们来看下不存在的情况,也就是lockedGetOrLoad()

Guava Cache学习笔记_第4张图片
加锁执行,控制并发
里面另一个方法看下一张图片
Guava Cache学习笔记_第5张图片
这里我们需要紧跟loader,因为他是一个包括加载,刷新等等函数一身。

Guava Cache学习笔记_第6张图片
Guava Cache学习笔记_第7张图片
到此源码也差不多结束了~

总结

  1. 从上面源码可以看到,当被get的时候才会去更新value,它不像定时器一样自动定时去刷新,过期回收。
  2. 我们需要设计好自动刷新的时间,减少返回给前端旧的脏数据。

参考文章

  • [Google Guava] 3-缓存.
  • Guava 源码分析之Cache的实现原理.

你可能感兴趣的:(Guava,Cache学习笔记)