2424package org .incendo .cloud ;
2525
2626import io .leangen .geantyref .TypeToken ;
27+ import java .util .ArrayDeque ;
2728import java .util .Collection ;
2829import java .util .Collections ;
30+ import java .util .Deque ;
31+ import java .util .HashMap ;
2932import java .util .HashSet ;
3033import java .util .LinkedList ;
3134import java .util .List ;
@@ -381,8 +384,8 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
381384 return Collections .unmodifiableSet (new HashSet <>(this .capabilities ));
382385 }
383386
384- private final ThreadLocal <Map <Pair <C , Permission >, PermissionResult >> threadLocalPermissionCache =
385- ThreadLocal .withInitial (ConcurrentHashMap ::new );
387+ private final ThreadLocal <Deque < Map <Pair <C , Permission >, PermissionResult > >> threadLocalPermissionCache =
388+ ThreadLocal .withInitial (ArrayDeque ::new );
386389
387390 @ SuppressWarnings ("rawtypes" )
388391 private @ NonNull <T > PermissionResult testPermissionCaching (
@@ -393,7 +396,8 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
393396 if (!this .settings .get (ManagerSetting .REDUCE_REDUNDANT_PERMISSION_CHECKS )) {
394397 return tester .apply (Pair .of (sender , permission ));
395398 }
396- return this .threadLocalPermissionCache .get ().computeIfAbsent ((Pair ) Pair .of (sender , permission ), (Function ) tester );
399+ return Objects .requireNonNull (this .threadLocalPermissionCache .get ().peek ())
400+ .computeIfAbsent ((Pair ) Pair .of (sender , permission ), (Function ) tester );
397401 }
398402
399403 /**
@@ -409,9 +413,10 @@ public boolean hasCapability(final @NonNull CloudCapability capability) {
409413 final @ NonNull Permission permission
410414 ) {
411415 try {
416+ this .threadLocalPermissionCache .get ().push (new HashMap <>());
412417 return this .testPermission_ (sender , permission );
413418 } finally {
414- this .threadLocalPermissionCache .get ().clear ();
419+ this .threadLocalPermissionCache .get ().pop ();
415420 }
416421 }
417422
0 commit comments