2017-02-10 11 views
0

db에서 매일로드되는 모든 고객 세부 정보가 포함 된 캐시가 있습니다. 하지만 일일 고객 정보를로드하기 전에 캐시의 이전 항목을 모두 삭제해야합니다.자바 구아바 캐시 : 모든 캐시 항목을 지우는 방법?

현재 내가 뭐하는 거지 :

public enum PeriodicUpdater { 

    TIMER; 
    private final AtomicBoolean isPublishing = new AtomicBoolean(false); 
    private final long   period  = TimeUnit.DAYS.toMillis(1); 

    @Autowired 
    @Qualifier("TestUtils") @Setter 
    private TestUtils testUtils; 

    public synchronized boolean initialize() { 
     return initialize(period, period); 
    } 


    boolean initialize(long delay, long period) { 
     if (isPublishing.get()) { 
      return false; 
     } 
     TimerTask task = new TimerTask() { 

      @Override public void run() { 
       try { 

        String path = getFile(); 
        if(TestUtils.getFileNameCache().getIfPresent(path) == null) { 
         TestUtils.setFileNameCache(testUtils.buildFileCache(path)); 
        } 
       } catch (Exception e) { 
        log.warn("Failed!", e); 
       } 
      } 
     }; 
     Timer timer = new Timer("PeriodicUpdater", true); // daemon=true 
     timer.schedule(task, delay, period); 
     isPublishing.set(true); 
     return true; 
    } 
} 

는 여기 캐시를 사용하고 있습니다 :

public class TestUtils { 

     private static Cache<String, Map<String, List<String>>> fileCache = CacheBuilder 
       .newBuilder() 
       .expireAfterWrite(4, TimeUnit.DAYS) 
       .build(); 


    public TestUtils() { 

      String path = getFile(); 
      fileNameCache = buildFileCache(path); 
      } 

    public Cache<String, String> buildFileCache(String path) { 

      Cache<String, String> fileList = CacheBuilder 
        .newBuilder() 
        .expireAfterWrite(4, TimeUnit.DAYS) 
        .build(); 

      fileList.put(path, new Date().toString()); 

      return fileList; 
     } 
/* doing some stuff with the cache */ 

     } 

이 일의 정확합니까? 캐시가 지워지지는 않습니다. 내가 틀렸다면 나를 교정 할 수 있습니까?

+1

'Cache'이 방법을'있다 모든 항목을 지우는 invalidateAll()'을 호출합니다. – shmosel

+0

Google 라이브러리를 사용하고 있으므로 [가로 맞춤] (https://google.github.io/)에서이 메모를 삭제하겠습니다. 스타일 가이드/javaguide.html # s4.6.3-horizontal-alignment) Google의 자바 스타일 가이드 – shmosel

+1

google 라이브러리를 사용하고 Google 코딩 스타일을 채택하는 것은 부적합합니다. (비록 내가 OP의 수평 정렬을 약간 혼란스럽게 만들었지 만 : P) –

답변

0

Cache.invalidateAll()은 현재 캐시에있는 모든 항목을 지 웁니다.

즉, 매일 항목을 다시로드하려는 경우 4 일마다 캐시의 내용 만 만료되는 이유는 무엇입니까? (.expireAfterWrite(4, TimeUnit.DAYS). 간단하게 변경 41 A와 하루에 한 번 내용을 다시로드합니다.

을 또한, 아드리안 셤 당신이 열거 형을 오용하고 언급 한 바와 같이. public enum PeriodicUpdater 거의 확실 public class PeriodicUpdater을해야합니다.