2017-01-31 4 views
0

J'ai une question. Si j'utilise expireAfterAccess et suppose que mon entrée expire après 2 heures. Maintenant, si j'appelle get() pour cette entrée après un certain temps (disons 5 heures), sera-t-il à nouveau mis en cache? ou va-t-il expirer pour de bon?Goyave cache expireAfterAccess question

private final LoadingCache<String, Map<String, PinPointRule>> pinPointRuleCache = CacheBuilder.newBuilder().maximumSize(500000) 
     .expireAfterAccess(2, TimeUnit.HOURS).build(new CacheLoader<String, Map<String, PinPointRule>>(){ 
      @Override 
      public Map<String, PinPointRule> load(String dummyToken) throws Exception { 
       return loadPinPointRules(dummyToken); 
      } 

      public ListenableFuture<Map<String,PinPointRule>> reload(final String key, final Map<String,PinPointRule> oldValue) throws Exception { 
       ListenableFutureTask<Map<String,PinPointRule>> task = ListenableFutureTask.create(new Callable<Map<String,PinPointRule>>() { 
        public Map<String,PinPointRule> call() throws Exception { 
         long start = System.nanoTime(); 
         LOGGER.info("LoadingCache Reload"); 
         try { 
          return loadPinPointRules(key); 
         } catch (Exception e) { 
          LOGGER.error("Error while loading pinpoint rules. Returning old value. Exception :: {}", getStackTrace(e)); 
         } finally { 
          LOGGER.info("Time taken in reloading pinpoint rule: {} ", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)); 
         } 
         return oldValue; 
        } 
       }); 
       executor.execute(task); 
       return task; 
      }; 
     }); 
+0

Il sera à nouveau mis en cache (c'est-à-dire que la méthode 'load' sur' CacheLoader' que vous aurez fourni sera appelée), c'est comme ça que fonctionnent les caches. – Xaerxess

+0

@Xaerxess: merci, je n'ai pas pu obtenir cette information par le biais du document. – Boola

Répondre

0

Comme Xaerxesscommented, « il se cache à nouveau (à savoir la méthode de charge sur CacheLoader vous avez fourni sera appelé), il est comment fonctionnent les caches. »

En cas de doute et la documentation ne sont pas assez clair, vous pouvez toujours tester:

@Test 
public void expireAfterAccessReloadsCache() throws Exception { 
    CacheLoader<Integer, String> cacheLoader = Mockito.mock(CacheLoader.class); 
    Integer testKey = 1; 
    String testValue = "1"; 
    when(cacheLoader.load(testKey)).thenReturn(testValue); 
    FakeTicker fakeTicker = new FakeTicker(); 
    LoadingCache<Integer, String> loadingCache = CacheBuilder.newBuilder() 
      .ticker(fakeTicker) 
      .expireAfterAccess(2, TimeUnit.HOURS) 
      .build(cacheLoader); 
    assert testValue.equals(loadingCache.get(testKey)); 
    verify(cacheLoader).load(testKey); 
    assert testValue.equals(loadingCache.get(testKey)); 
    verifyZeroInteractions(cacheLoader); 
    fakeTicker.advance(1, TimeUnit.HOURS); 
    assert testValue.equals(loadingCache.get(testKey)); 
    verifyZeroInteractions(cacheLoader); 
    fakeTicker.advance(4, TimeUnit.HOURS); 
    assert testValue.equals(loadingCache.get(testKey)); 
    verify(cacheLoader, times(2)).load(testKey); 
    assert testValue.equals(loadingCache.get(testKey)); 
    verifyZeroInteractions(cacheLoader); 
} 

Il est certainement pas quelque chose que vous mettriez dans vos tests, mais ce type d'exercice peut être très utile pour mieux comprendre comment fonctionne une bibliothèque.

Vous pouvez également apprendre comment une classe fonctionne en lisant/en passant par ses tests unitaires/fonctionnels. par exemple. guava/CacheExpirationTest.java at master · google/guava.