Commit 626d2ce194f5ca0beea5d78d32e89bcfc906210a
1 parent
0f14a36c
caffeine cache max size should include value collection size
Showing
1 changed file
with
12 additions
and
1 deletions
@@ -18,6 +18,7 @@ package org.thingsboard.server.dao.cache; | @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.cache; | ||
18 | import com.github.benmanes.caffeine.cache.Caffeine; | 18 | import com.github.benmanes.caffeine.cache.Caffeine; |
19 | import com.github.benmanes.caffeine.cache.RemovalCause; | 19 | import com.github.benmanes.caffeine.cache.RemovalCause; |
20 | import com.github.benmanes.caffeine.cache.Ticker; | 20 | import com.github.benmanes.caffeine.cache.Ticker; |
21 | +import com.github.benmanes.caffeine.cache.Weigher; | ||
21 | import lombok.Data; | 22 | import lombok.Data; |
22 | import lombok.extern.slf4j.Slf4j; | 23 | import lombok.extern.slf4j.Slf4j; |
23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
@@ -31,6 +32,7 @@ import org.springframework.context.annotation.Bean; | @@ -31,6 +32,7 @@ import org.springframework.context.annotation.Bean; | ||
31 | import org.springframework.context.annotation.Configuration; | 32 | import org.springframework.context.annotation.Configuration; |
32 | 33 | ||
33 | import java.util.Arrays; | 34 | import java.util.Arrays; |
35 | +import java.util.Collection; | ||
34 | import java.util.List; | 36 | import java.util.List; |
35 | import java.util.Map; | 37 | import java.util.Map; |
36 | import java.util.concurrent.TimeUnit; | 38 | import java.util.concurrent.TimeUnit; |
@@ -64,8 +66,9 @@ public class CaffeineCacheConfiguration { | @@ -64,8 +66,9 @@ public class CaffeineCacheConfiguration { | ||
64 | private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) { | 66 | private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) { |
65 | final Caffeine<Object, Object> caffeineBuilder | 67 | final Caffeine<Object, Object> caffeineBuilder |
66 | = Caffeine.newBuilder() | 68 | = Caffeine.newBuilder() |
69 | + .weigher(collectionSafeWeigher()) | ||
70 | + .maximumWeight(cacheSpec.getMaxSize()) | ||
67 | .expireAfterWrite(cacheSpec.getTimeToLiveInMinutes(), TimeUnit.MINUTES) | 71 | .expireAfterWrite(cacheSpec.getTimeToLiveInMinutes(), TimeUnit.MINUTES) |
68 | - .maximumSize(cacheSpec.getMaxSize()) | ||
69 | .ticker(ticker()); | 72 | .ticker(ticker()); |
70 | return new CaffeineCache(name, caffeineBuilder.build()); | 73 | return new CaffeineCache(name, caffeineBuilder.build()); |
71 | } | 74 | } |
@@ -80,4 +83,12 @@ public class CaffeineCacheConfiguration { | @@ -80,4 +83,12 @@ public class CaffeineCacheConfiguration { | ||
80 | return new PreviousDeviceCredentialsIdKeyGenerator(); | 83 | return new PreviousDeviceCredentialsIdKeyGenerator(); |
81 | } | 84 | } |
82 | 85 | ||
86 | + private Weigher<? super Object, ? super Object> collectionSafeWeigher() { | ||
87 | + return (Weigher<Object, Object>) (key, value) -> { | ||
88 | + if(value instanceof Collection) { | ||
89 | + return ((Collection) value).size(); | ||
90 | + } | ||
91 | + return 1; | ||
92 | + }; | ||
93 | + } | ||
83 | } | 94 | } |