mirror of https://github.com/halo-dev/halo
Run a timer for cache cleaner
parent
baefbf220c
commit
e5f5a66c75
|
@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
@ -17,6 +18,11 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||
@Slf4j
|
||||
public class InMemoryCacheStore extends StringCacheStore {
|
||||
|
||||
/**
|
||||
* Cleaner schedule period. (ms)
|
||||
*/
|
||||
private final static long PERIOD = 60 * 1000;
|
||||
|
||||
/**
|
||||
* Cache container.
|
||||
*/
|
||||
|
@ -27,6 +33,11 @@ public class InMemoryCacheStore extends StringCacheStore {
|
|||
*/
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
public InMemoryCacheStore() {
|
||||
// Run a cache store cleaner
|
||||
new Timer().scheduleAtFixedRate(new CacheExpiryCleaner(), 0, PERIOD);
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<CacheWrapper<String>> getInternal(String key) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
|
@ -79,17 +90,18 @@ public class InMemoryCacheStore extends StringCacheStore {
|
|||
}
|
||||
|
||||
/**
|
||||
* Cache store cleaner.
|
||||
* Cache cleaner.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 03/28/19
|
||||
*/
|
||||
private class CacheStoreCleaner extends TimerTask {
|
||||
private class CacheExpiryCleaner extends TimerTask {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
log.trace("Cache clean task is cleaning");
|
||||
cacheContainer.keySet().forEach(InMemoryCacheStore.this::get);
|
||||
log.trace("Cache lean task cleaned");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue