26
26
import java .util .Optional ;
27
27
import java .util .Set ;
28
28
import java .util .concurrent .ConcurrentMap ;
29
+ import java .util .concurrent .atomic .AtomicInteger ;
29
30
import java .util .function .BiConsumer ;
30
31
import java .util .function .BiPredicate ;
31
32
import java .util .function .Function ;
32
33
import java .util .function .Supplier ;
33
- import java .util .stream .Collectors ;
34
34
35
35
abstract class AbstractStroomCache <K , V > implements StroomCache <K , V > {
36
36
@@ -39,6 +39,7 @@ abstract class AbstractStroomCache<K, V> implements StroomCache<K, V> {
39
39
private final String name ;
40
40
private final Supplier <CacheConfig > cacheConfigSupplier ;
41
41
private final BiConsumer <K , V > removalNotificationConsumer ;
42
+ private final AtomicInteger reachedSizeLimitCount = new AtomicInteger ();
42
43
43
44
protected volatile CacheHolder <K , V > cacheHolder = null ;
44
45
@@ -73,6 +74,7 @@ public AbstractStroomCache(final String name,
73
74
*/
74
75
abstract Cache <K , V > createCacheFromBuilder (final Caffeine <K , V > cacheBuilder );
75
76
77
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
76
78
@ Override
77
79
public synchronized void rebuild () {
78
80
LOGGER .trace (() -> buildMessage ("rebuild" ));
@@ -143,11 +145,10 @@ private RemovalListener<K, V> createRemovalListener() {
143
145
cause + ")" ;
144
146
145
147
if (cause == RemovalCause .SIZE ) {
146
- LOGGER .warn (() -> "Cache reached size limit '" + name + "'" );
147
- LOGGER .debug (messageSupplier );
148
- } else {
149
- LOGGER .trace (messageSupplier );
148
+ reachedSizeLimitCount .incrementAndGet ();
149
+ LOGGER .debug (() -> "Cache reached size limit '" + name + "'" );
150
150
}
151
+ LOGGER .trace (messageSupplier );
151
152
removalNotificationConsumer .accept (key , value );
152
153
};
153
154
}
@@ -246,7 +247,7 @@ public void invalidateEntries(final BiPredicate<K, V> entryPredicate) {
246
247
LOGGER .trace (() -> buildMessage ("invalidateEntries" , entry .getKey ()));
247
248
return entry .getKey ();
248
249
})
249
- .collect ( Collectors . toList () );
250
+ .toList ();
250
251
251
252
// Doing the remove on the cacheAsMap means if rebuild was called we are still operating on
252
253
// the old cache
@@ -304,6 +305,9 @@ public CacheInfo getCacheInfo() {
304
305
// We don't make use of Weighers in the cache so the weight stats are meaningless
305
306
map .remove ("EvictionWeight" );
306
307
308
+ // Let users know how many times we have evicted items for hitting the size limit.
309
+ map .put ("Reached Size Limit Count" , Integer .toString (reachedSizeLimitCount .get ()));
310
+
307
311
return new CacheInfo (name , basePropertyPath , map );
308
312
}
309
313
@@ -331,10 +335,10 @@ private void addEntries(final Map<String, String> map, String string) {
331
335
if (kv .length == 2 ) {
332
336
String key = kv [0 ].replaceAll ("\\ W" , "" );
333
337
String value = kv [1 ].replaceAll ("\\ D" , "" );
334
- if (key .length () > 0 ) {
338
+ if (! key .isEmpty () ) {
335
339
final char [] chars = key .toCharArray ();
336
340
chars [0 ] = Character .toUpperCase (chars [0 ]);
337
- key = new String (chars , 0 , chars . length );
341
+ key = new String (chars );
338
342
}
339
343
340
344
map .put (key , value );
0 commit comments