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 @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
instanceId2JobInfoCache = CacheBuilder.newBuilder() instanceId2JobInfoCache = CacheBuilder.newBuilder()
.softValues()
.concurrencyLevel(CACHE_CONCURRENCY_LEVEL) .concurrencyLevel(CACHE_CONCURRENCY_LEVEL)
.maximumSize(instanceMetadataCacheSize) .maximumSize(instanceMetadataCacheSize)
.build(); .build();

View File

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

View File

@ -105,7 +105,7 @@ public class DingTalkAlarmService implements Alarmable {
} }
this.agentId = Long.valueOf(agentId); this.agentId = Long.valueOf(agentId);
dingTalkUtils = new DingTalkUtils(appKey, appSecret); 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!"); log.info("[DingTalkAlarmService] init DingTalkAlarmService successfully!");
} }

View File

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