Created
January 23, 2016 14:19
-
-
Save 0532/bf97ca65e1d18dd9b6ed to your computer and use it in GitHub Desktop.
guava caches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package io.terminus.neverest.notice.dao.site.caches; | |
| import com.google.common.cache.Cache; | |
| import com.google.common.cache.CacheBuilder; | |
| import com.google.common.cache.CacheLoader; | |
| import com.google.common.cache.LoadingCache; | |
| import org.junit.Test; | |
| import java.util.concurrent.Callable; | |
| /** | |
| * Created by Wang Lichao at 16/1/23 21:34. | |
| * Desc: | |
| * 回收的参数 | |
| * 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) | |
| * | |
| * refresh机制: | |
| * 1. LoadingCache.refresh(K) 在生成新的value的时候,旧的value依然会被使用。 | |
| * 2. CacheLoader.reload(K, V) 生成新的value过程中允许使用旧的value | |
| * 3. CacheBuilder.refreshAfterWrite(long, TimeUnit) 自动刷新cache | |
| * | |
| * http://www.cnblogs.com/peida/p/Guava_Cache.html | |
| */ | |
| public class CachesTest { | |
| @Test | |
| public void TestLoadingCache() throws Exception{ | |
| LoadingCache<String,String> cahceBuilder= CacheBuilder | |
| .newBuilder() | |
| .build(new CacheLoader<String, String>(){ | |
| @Override | |
| public String load(String key) throws Exception { | |
| String strProValue="hello "+key+"!"; | |
| return strProValue; | |
| } | |
| }); | |
| System.out.println("jerry value:"+cahceBuilder.apply("jerry")); | |
| System.out.println("jerry value:"+cahceBuilder.get("jerry")); | |
| System.out.println("peida value:"+cahceBuilder.get("peida")); | |
| System.out.println("peida value:"+cahceBuilder.apply("peida")); | |
| System.out.println("lisa value:"+cahceBuilder.apply("lisa")); | |
| cahceBuilder.put("harry", "ssdded"); | |
| System.out.println("harry value:"+cahceBuilder.get("harry")); | |
| } | |
| @Test | |
| public void testcallableCache()throws Exception{ | |
| Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(1000).build(); | |
| String resultVal = cache.get("jerry", new Callable<String>() { | |
| public String call() { | |
| String strProValue="hello "+"jerry"+"!"; | |
| return strProValue; | |
| } | |
| }); | |
| System.out.println("jerry value : " + resultVal); | |
| resultVal = cache.get("peida", new Callable<String>() { | |
| public String call() { | |
| String strProValue="hello "+"peida"+"!"; | |
| return strProValue; | |
| } | |
| }); | |
| System.out.println("peida value : " + resultVal); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment