标签:guava cache 移除监听器
显式清除例如:(使用方式)
public <K, V> LoadingCache<K, V> cached(CacheLoader<K, V> cacheLoader) { LoadingCache<K, V> cache = CacheBuilder.newBuilder().maximumSize(2).weakKeys().softValues().refreshAfterWrite(120, TimeUnit.SECONDS).expireAfterWrite(10, TimeUnit.MINUTES).removalListener(new RemovalListener<K, V>() { @Override public void onRemoval(RemovalNotification<K, V> rn) { System.out.println(rn.getKey() + "被移除"); } }).build(cacheLoader); return cache; } /** * 通过key获取value 调用方式 commonCache.get(key) ; return String * * @param key * @return * @throws Exception */ public LoadingCache<String, String> commonCache(final String key) throws Exception { LoadingCache<String, String> commonCache = cached(new CacheLoader<String, String>() { @Override public String load(String key) throws Exception { return "hello " + key + "!"; } }); return commonCache; } @Test public void testCache() throws Exception { LoadingCache<String, String> commonCache = commonCache("1"); System.out.println("1:" + commonCache.get("1")); // commonCache.apply("harry"); System.out.println("2:" + commonCache.get("2")); // commonCache.apply("lisa"); System.out.println("3:" + commonCache.get("3")); commonCache.invalidateAll(); }
标签:guava cache 移除监听器
原文地址:http://blog.csdn.net/u012516914/article/details/43484015