feat: use soft reference in cache to optimize memory usage #291

This commit is contained in:
tjq 2021-11-03 21:45:50 +08:00
parent c3eefeb626
commit 6bcd6e0be3
4 changed files with 7 additions and 1 deletions

View File

@ -38,6 +38,7 @@ public class InstanceMetadataService implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
instanceId2JobInfoCache = CacheBuilder.newBuilder()
.softValues()
.concurrencyLevel(CACHE_CONCURRENCY_LEVEL)
.maximumSize(instanceMetadataCacheSize)
.build();

View File

@ -39,19 +39,23 @@ public class CacheService {
public CacheService() {
jobId2JobNameCache = CacheBuilder.newBuilder()
.softValues()
.expireAfterWrite(Duration.ofMinutes(1))
.maximumSize(512)
.build();
workflowId2WorkflowNameCache = CacheBuilder.newBuilder()
.softValues()
.expireAfterWrite(Duration.ofMinutes(1))
.maximumSize(512)
.build();
instanceId2AppId = CacheBuilder.newBuilder()
.softValues()
.maximumSize(1024)
.build();
jobId2AppId = CacheBuilder.newBuilder()
.softValues()
.maximumSize(1024)
.build();
}

View File

@ -105,7 +105,7 @@ public class DingTalkAlarmService implements Alarmable {
}
this.agentId = Long.valueOf(agentId);
dingTalkUtils = new DingTalkUtils(appKey, appSecret);
mobile2UserIdCache = CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).build();
mobile2UserIdCache = CacheBuilder.newBuilder().softValues().maximumSize(CACHE_SIZE).build();
log.info("[DingTalkAlarmService] init DingTalkAlarmService successfully!");
}

View File

@ -18,6 +18,7 @@ public class LRUCache<K, V> {
public LRUCache(int cacheSize) {
innerCache = CacheBuilder.newBuilder()
.softValues()
.concurrencyLevel(2)
.maximumSize(cacheSize)
.build();