add
메서드를 호출하여 여러 스레드에서 구아바 캐시를 채울 예정입니다. 이제 매 30 초마다 실행되는 백그라운드 스레드에서 캐시에있는 모든 내용을 원자화 방식으로 sendToDB
메서드로 보내려고합니다.Guava 캐시를 다른 방법으로 보내는 동안 30 초마다 비우는 방법?
public class Example {
private final ScheduledExecutorService executorService = Executors
.newSingleThreadScheduledExecutor();
private final Cache<Integer, List<Process>> cache = CacheBuilder.newBuilder().maximumSize(100000)
.removalListener(RemovalListeners.asynchronous(new CustomRemovalListener(), executorService))
.build();
private static class Holder {
private static final Example INSTANCE = new Example();
}
public static Example getInstance() {
return Holder.INSTANCE;
}
private Example() {
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// is this the right way to send cache map?
sendToDB(cache.asMap());
}
}, 0, 30, SECONDS);
}
// this method will be called from multiple threads
public void add(final int id, final Process process) {
// add id and process into cache
}
// this will only be called from single background thread
private void sendToDB(ConcurrentMap<Integer, List<Process>> holder) {
// use holder here
}
}
이 내 sendToDB
방법에 cache
지도를 보낼 수있는 올바른 방법인가 : 다음
나는 방법으로 캐시에서 일어나는 모든 변경 사항을 반영하므로 캐시를 비우지 않으므로 cache.asMap()
이 올바른 방법이 아닐 수도 있다고 생각하십니까?
'asMap'은 기억이 잘 나면 내용의 복사본을 반환하고, 1/asMap 만 복사하면 메서드 –
@RC를 호출합니다. 아니, 그냥보기에요. – shmosel
'add()'메소드 (적어도 개요)를 채울 수 있습니까? Java 8을 사용하고 있습니까? 지도를 삭제해야합니까, 아니면 열쇠를 남겨두고 값을 재설정 할 수 있습니까? – shmosel