差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
下次修改 Both sides next revision
java:guava:cache:helloworld [2016/04/13 13:40]
tony 建立
java:guava:cache:helloworld [2016/04/13 23:30]
tony
行 1: 行 1:
 {{tag>​java guava}} {{tag>​java guava}}
 ====== Guava Cache Hello World ====== ====== Guava Cache Hello World ======
 +===== Introduction =====
 +大部分應用程式都會透過Cache機制,去增加效能或暫時性的備援。Guava提供了簡單的Cache實做,讓你不用替資源回收或同步問題煩惱。
 ===== How to? ===== ===== How to? =====
 +我們可以透過CacheBuilder去設定Cache的條件。在build時,你可以選擇要預先提供服務實做,或是在get時提供:​
 <code java> <code java>
 package org.tonylin.practice.guava.cache;​ package org.tonylin.practice.guava.cache;​
行 15: 行 18:
  
 public class TestCache { public class TestCache {
 + @Test 
 + public void testCache() throws Exception { 
 + Cache<​String,​ String> cache =  CacheBuilder.newBuilder() 
 + .maximumSize(10) 
 + .expireAfterAccess(5,​ TimeUnit.SECONDS) 
 + .expireAfterWrite(5,​ TimeUnit.SECONDS) 
 + .softValues() 
 + .build();​ 
 +  
 + Assert.assertEquals("​0",​ cache.get("​test1",​ ()->​{return "​0";​}));​ 
 + Thread.sleep(4000);​ 
 + Assert.assertEquals("​0",​ cache.get("​test1",​ ()->​{return "​1";​}));​ 
 + Thread.sleep(2000);​ 
 + Assert.assertNull(cache.getIfPresent("​test1"​));​ 
 + Assert.assertEquals("​2",​ cache.get("​test1",​ ()->​{return "​2";​}));​ 
 + }
  @Test  @Test
  public void test() throws Exception {  public void test() throws Exception {