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 | 18 | import com.github.benmanes.caffeine.cache.Caffeine; |
19 | 19 | import com.github.benmanes.caffeine.cache.RemovalCause; |
20 | 20 | import com.github.benmanes.caffeine.cache.Ticker; |
21 | +import com.github.benmanes.caffeine.cache.Weigher; | |
21 | 22 | import lombok.Data; |
22 | 23 | import lombok.extern.slf4j.Slf4j; |
23 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
... | ... | @@ -31,6 +32,7 @@ import org.springframework.context.annotation.Bean; |
31 | 32 | import org.springframework.context.annotation.Configuration; |
32 | 33 | |
33 | 34 | import java.util.Arrays; |
35 | +import java.util.Collection; | |
34 | 36 | import java.util.List; |
35 | 37 | import java.util.Map; |
36 | 38 | import java.util.concurrent.TimeUnit; |
... | ... | @@ -64,8 +66,9 @@ public class CaffeineCacheConfiguration { |
64 | 66 | private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) { |
65 | 67 | final Caffeine<Object, Object> caffeineBuilder |
66 | 68 | = Caffeine.newBuilder() |
69 | + .weigher(collectionSafeWeigher()) | |
70 | + .maximumWeight(cacheSpec.getMaxSize()) | |
67 | 71 | .expireAfterWrite(cacheSpec.getTimeToLiveInMinutes(), TimeUnit.MINUTES) |
68 | - .maximumSize(cacheSpec.getMaxSize()) | |
69 | 72 | .ticker(ticker()); |
70 | 73 | return new CaffeineCache(name, caffeineBuilder.build()); |
71 | 74 | } |
... | ... | @@ -80,4 +83,12 @@ public class CaffeineCacheConfiguration { |
80 | 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 | } | ... | ... |