Skip to content

Commit

Permalink
fix( ): error create lru-cache-timeout-cleaner thread (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clownsw authored Dec 21, 2023
1 parent 60e3681 commit bab9499
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

/**
* lru common resource cache
* lru common resource cache
*
* @author tomsun28
*/
@Slf4j
Expand Down Expand Up @@ -66,29 +63,32 @@ public class CommonCache {
*/
private ThreadPoolExecutor cleanTimeoutExecutor;

private CommonCache() { init();}

private CommonCache() {
init();
}

private void init() {
cacheMap = new ConcurrentLinkedHashMap
.Builder<>()
.maximumWeightedCapacity(DEFAULT_MAX_CAPACITY)
.listener((key, value) -> {
timeoutMap.remove(key);
if (value instanceof CacheCloseable) {
((CacheCloseable)value).close();
((CacheCloseable) value).close();
}
log.info("lru cache discard key: {}, value: {}.", key, value);
}).build();
timeoutMap = new ConcurrentHashMap<>(DEFAULT_MAX_CAPACITY >> 6);
cleanTimeoutExecutor = new ThreadPoolExecutor(1, 1,
1, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1), r -> new Thread("lru-cache-timeout-cleaner"),
new ArrayBlockingQueue<>(1),
r -> new Thread(r, "lru-cache-timeout-cleaner"),
new ThreadPoolExecutor.DiscardOldestPolicy());
// init monitor available detector cyc task
ScheduledThreadPoolExecutor scheduledExecutor = new ScheduledThreadPoolExecutor(1,
ScheduledThreadPoolExecutor scheduledExecutor = new ScheduledThreadPoolExecutor(1,
r -> new Thread(r, "lru-cache-available-detector"));
scheduledExecutor.scheduleWithFixedDelay(this::detectCacheAvailable,
2,20, TimeUnit.MINUTES);
2, 20, TimeUnit.MINUTES);
}

/**
Expand All @@ -104,7 +104,7 @@ private void detectCacheAvailable() {
cacheMap.remove(key);
timeoutMap.remove(key);
if (value instanceof CacheCloseable) {
((CacheCloseable)value).close();
((CacheCloseable) value).close();
}

}
Expand Down Expand Up @@ -132,7 +132,7 @@ private void cleanTimeoutCache() {
cacheMap.remove(key);
if (value instanceof CacheCloseable) {
log.warn("[cache] close the timeout cache, key {}", key);
((CacheCloseable)value).close();
((CacheCloseable) value).close();
}
}
});
Expand All @@ -143,8 +143,9 @@ private void cleanTimeoutCache() {

/**
* add update cache
* @param key cache key
* @param value cache value
*
* @param key cache key
* @param value cache value
* @param timeDiff cache time millis
*/
public void addCache(Object key, Object value, Long timeDiff) {
Expand All @@ -166,7 +167,8 @@ public void addCache(Object key, Object value, Long timeDiff) {

/**
* add update cache
* @param key cache key
*
* @param key cache key
* @param value cache value
*/
public void addCache(Object key, Object value) {
Expand All @@ -175,7 +177,8 @@ public void addCache(Object key, Object value) {

/**
* get cache by key
* @param key cache key
*
* @param key cache key
* @param refreshCache is refresh cache
* @return cache object
*/
Expand Down Expand Up @@ -205,18 +208,20 @@ public Optional<Object> getCache(Object key, boolean refreshCache) {

/**
* remove cache by key
*
* @param key key
*/
public void removeCache(Object key) {
timeoutMap.remove(key);
Object value = cacheMap.remove(key);
if (value instanceof CacheCloseable) {
((CacheCloseable)value).close();
((CacheCloseable) value).close();
}
}

/**
* get common cache instance
*
* @return cache
*/
public static CommonCache getInstance() {
Expand All @@ -227,6 +232,6 @@ public static CommonCache getInstance() {
* static instance
*/
private static class SingleInstance {
private static final CommonCache INSTANCE= new CommonCache();
private static final CommonCache INSTANCE = new CommonCache();
}
}

0 comments on commit bab9499

Please sign in to comment.