|
|
|
@ -18,6 +18,7 @@ package org.thingsboard.server.dao.cache; |
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine; |
|
|
|
import com.github.benmanes.caffeine.cache.RemovalCause; |
|
|
|
import com.github.benmanes.caffeine.cache.Ticker; |
|
|
|
import com.github.benmanes.caffeine.cache.Weigher; |
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
|
|
@ -31,6 +32,7 @@ import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
@ -64,8 +66,9 @@ public class CaffeineCacheConfiguration { |
|
|
|
private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) { |
|
|
|
final Caffeine<Object, Object> caffeineBuilder |
|
|
|
= Caffeine.newBuilder() |
|
|
|
.weigher(collectionSafeWeigher()) |
|
|
|
.maximumWeight(cacheSpec.getMaxSize()) |
|
|
|
.expireAfterWrite(cacheSpec.getTimeToLiveInMinutes(), TimeUnit.MINUTES) |
|
|
|
.maximumSize(cacheSpec.getMaxSize()) |
|
|
|
.ticker(ticker()); |
|
|
|
return new CaffeineCache(name, caffeineBuilder.build()); |
|
|
|
} |
|
|
|
@ -80,4 +83,12 @@ public class CaffeineCacheConfiguration { |
|
|
|
return new PreviousDeviceCredentialsIdKeyGenerator(); |
|
|
|
} |
|
|
|
|
|
|
|
private Weigher<? super Object, ? super Object> collectionSafeWeigher() { |
|
|
|
return (Weigher<Object, Object>) (key, value) -> { |
|
|
|
if(value instanceof Collection) { |
|
|
|
return ((Collection) value).size(); |
|
|
|
} |
|
|
|
return 1; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|