Guava缓存-Cache

Guava Cache有两种建立方式:java

  1. cacheLoader
  2. callable callback缓存

  经过这两种方法建立的cache,和一般用map来缓存的作法比,不一样在于,这两种方法都实现了一种逻辑——从缓存中取key X的值,若是该值已经缓存过了,则返回缓存中的值,若是没有缓存过,能够经过某个方法来获取这个值。但不一样的在于cacheloader的定义比较宽泛,是针对整个cache定义的,能够认为是统一的根据key值load value的方法。而callable的方式较为灵活,容许你在get的时候指定。异步

示例async

import com.google.common.cache.*;
import org.junit.Test;

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

/**
 * @author Kevin
 * @description
 * @date 2016/8/5
 */
public class CacheTest {

    @Test
    public void cacheLoaderTest() throws Exception {
        CacheLoader<String, String> cacheLoader = new CacheLoaderImpl();

        LoadingCache<String, String> cacheBuilder = CacheBuilder.newBuilder().maximumSize(1000).removalListener(new RemovalListenerImpl()).
                expireAfterAccess(5L, TimeUnit.SECONDS).build(cacheLoader);

        // read from database
        System.out.println("Kevin value:" + cacheBuilder.get("Kevin"));
        System.out.println("Han value:" + cacheBuilder.getUnchecked("Han"));

        // read from cache
        System.out.println("Kevin value:" + cacheBuilder.get("Kevin"));
        System.out.println("Han value:" + cacheBuilder.getUnchecked("Han"));

        // wait cache expire
        Thread.sleep(10000);

        // cache miss get EXPIRED notification
        System.out.println("Kevin value:" + cacheBuilder.get("Kevin"));
        System.out.println("Han value:" + cacheBuilder.getUnchecked("Han"));

        cacheBuilder.put("name", "Kevin");
        System.out.println("name value:" + cacheBuilder.get("name"));

        // async replace the key value ,get REPLACED notification
        cacheBuilder.refresh("name");

        // invalidate a key get EXPLICIT notification
        cacheBuilder.invalidate("name");
        System.out.println("name value:" + cacheBuilder.get("name"));

        // reload from databse then return value
        System.out.println(cacheLoader.load("name"));
    }


    @Test
    public void callbackCacheTest() throws ExecutionException {
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(1000).build();
        String result = cache.get("Kevin", new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("call me when cache miss");
                String value = "hello " + "Kevin" + "!";
                return value;
            }
        });
        System.out.println("Kevin value:" + result);

        result = cache.get("Kevin", new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("call me when cache miss");
                String value = "hello " + "Kevin" + "!";
                return value;
            }
        });
        System.out.println("Kevin value:" + result);
    }
}

class CacheLoaderImpl extends CacheLoader<String, String> {

    @Override
    public String load(String key) throws Exception {
        System.out.println("call me when cache miss,read from database...");
        String value = "hello " + key + "!";
        return value;
    }
}

// 缓存事件监听器
class RemovalListenerImpl implements RemovalListener<String, String> {

    @Override
    public void onRemoval(RemovalNotification<String, String> notification) {
        System.out.println("cause : " + notification.getCause() + ",key : " + notification.getKey() + ",value : " + notification.getValue());
    }
}

cache的参数说明:ide

  回收的参数:
  1. 大小的设置:CacheBuilder.maximumSize(long)  CacheBuilder.weigher(Weigher)  CacheBuilder.maxumumWeigher(long)
  2. 时间:expireAfterAccess(long, TimeUnit) expireAfterWrite(long, TimeUnit)
  3. 引用:CacheBuilder.weakKeys() CacheBuilder.weakValues()  CacheBuilder.softValues()
  4. 明确的删除:invalidate(key)  invalidateAll(keys)  invalidateAll()
  5. 缓存监听器:CacheBuilder.removalListener(RemovalListener)
  ui

  refresh机制:
  1. LoadingCache.refresh(K)  异步刷新对应key的值。
  2. CacheLoader.reload(K, V) 从新加载key的值并返回新的value。
  3. CacheBuilder.refreshAfterWrite(long, TimeUnit) 自动刷新cache。google

guava Cache数据移除:code

  1. 基于容量大小的移除,定义的方式通常为 CacheBuilder.maximumSize(long)
  2. 基于时间的移除,guava提供了两个基于时间移除的方法
        expireAfterAccess(long, TimeUnit)  这个方法是根据某个键值对最后一次访问以后多少时间后移除
        expireAfterWrite(long, TimeUnit)  这个方法是根据某个键值对被建立或值被替换后多少时间移除
  3. 基于引用的移除,这种移除方式主要是基于java的垃圾回收机制,根据键或者值的引用关系决定移除

自主移除,主动移除有三种方法:
  1.单独移除用 Cache.invalidate(key)
  2.批量移除用 Cache.invalidateAll(keys)
  3.移除全部用 Cache.invalidateAll()事件

若是须要在移除数据的时候有所动做还能够定义Removal Listener,可是有点须要注意的是默认Removal Listener中的行为是和移除动做同步执行的,若是须要改为异步形式,能够考虑使用RemovalListeners.asynchronous(RemovalListener, Executor)ip

相关文章
相关标签/搜索