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 */
}
이 일의 정확합니까? 캐시가 지워지지는 않습니다. 내가 틀렸다면 나를 교정 할 수 있습니까?
'Cache'이 방법을'있다 모든 항목을 지우는 invalidateAll()'을 호출합니다. – shmosel
Google 라이브러리를 사용하고 있으므로 [가로 맞춤] (https://google.github.io/)에서이 메모를 삭제하겠습니다. 스타일 가이드/javaguide.html # s4.6.3-horizontal-alignment) Google의 자바 스타일 가이드 – shmosel
google 라이브러리를 사용하고 Google 코딩 스타일을 채택하는 것은 부적합합니다. (비록 내가 OP의 수평 정렬을 약간 혼란스럽게 만들었지 만 : P) –