Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(*): error create lru-cache-timeout-cleaner thread #1438

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading