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