33import java .time .Duration ;
44import java .time .Instant ;
55import java .util .ArrayList ;
6- import java .util .Collections ;
76import java .util .Comparator ;
87import java .util .HashMap ;
98import java .util .List ;
109import java .util .Map ;
1110import java .util .concurrent .atomic .AtomicBoolean ;
1211import java .util .stream .Collectors ;
1312
14- import com .fasterxml .jackson .core .type .TypeReference ;
15- import com .fasterxml .jackson .databind .ObjectMapper ;
1613import org .apache .commons .collections .CollectionUtils ;
17- import org .springframework .beans .factory .annotation .Autowired ;
1814import org .springframework .beans .factory .annotation .Value ;
19- import org .springframework .expression .spel .ast .BooleanLiteral ;
2015import org .springframework .stereotype .Component ;
2116
2217import com .github .benmanes .caffeine .cache .Cache ;
2318import com .github .benmanes .caffeine .cache .Caffeine ;
2419import com .igot .cb .cassandra .CassandraOperation ;
2520import com .igot .cb .util .Constants ;
2621
22+ import jakarta .annotation .PostConstruct ;
2723import lombok .extern .slf4j .Slf4j ;
2824
2925@ Component
@@ -36,19 +32,27 @@ public class CbPlanCacheMgr {
3632 @ Value ("${cb.plan.batch.size:5}" ) //default fallback to 5 if missing
3733 private int planBatchSize ;
3834
35+ @ Value ("${cb.plan.caffine.cache.max.size:5000}" )
36+ private int maxCacheSize ;
37+
3938 private final CassandraOperation cassandraOperation ;
4039 private Cache <String , List <Map <String , Object >>> cbPlanCache ;
4140
42- public CbPlanCacheMgr ( CassandraOperation cassandraOperation ) {
43- this . cassandraOperation = cassandraOperation ;
41+ @ PostConstruct
42+ public void initCache () {
4443 this .cbPlanCache = Caffeine .newBuilder ()
45- .maximumSize (1000 )
44+ .maximumSize (maxCacheSize )
4645 .expireAfterWrite (Duration .ofMinutes (ttlMinutes ))
4746 .build ();
4847 }
48+
49+ public CbPlanCacheMgr (CassandraOperation cassandraOperation ) {
50+ this .cassandraOperation = cassandraOperation ;
51+ }
4952
5053 private List <Map <String , Object >> getCbPlanForAll () {
51- List <Map <String , Object >> allCbPlanList = cbPlanCache .getIfPresent ("all-lookup" );
54+ String redisCacheKey = "all-lookup" ;
55+ List <Map <String , Object >> allCbPlanList = cbPlanCache .getIfPresent (redisCacheKey );
5256 if (allCbPlanList == null ) {
5357 log .info ("No CB Plans for all orgs in Cache, reading from Cassandra" );
5458 Map <String , Object > propertiesMap = new HashMap <>();
@@ -64,15 +68,16 @@ private List<Map<String, Object>> getCbPlanForAll() {
6468 }
6569 allCbPlanList = allCbPlanList .stream ()
6670 .filter (plan -> Boolean .TRUE .equals (plan .get (Constants .IS_ACTIVE ))).collect (Collectors .toList ());
67- cbPlanCache .put ("all" , allCbPlanList );
71+ cbPlanCache .put (redisCacheKey , allCbPlanList );
6872 } else {
6973 log .info ("Cache hit for all orgs: Found {} records" , allCbPlanList .size ());
7074 }
7175 return allCbPlanList ;
7276 }
7377
7478 private List <Map <String , Object >> getCbPlanForOrgId (String orgId ) {
75- List <Map <String , Object >> cbPlanList = cbPlanCache .getIfPresent (orgId + "-lookup" );
79+ String redisCacheKey = orgId + "-lookup" ;
80+ List <Map <String , Object >> cbPlanList = cbPlanCache .getIfPresent (redisCacheKey );
7681
7782 if (cbPlanList == null ) {
7883 log .info ("No CB Plans for orgId in Cache: {}, reading from Cassandra" , orgId );
@@ -89,7 +94,7 @@ private List<Map<String, Object>> getCbPlanForOrgId(String orgId) {
8994 }
9095 cbPlanList = cbPlanList .stream ()
9196 .filter (plan -> Boolean .TRUE .equals (plan .get (Constants .IS_ACTIVE ))).collect (Collectors .toList ());
92- cbPlanCache .put (orgId + "-lookup" , cbPlanList );
97+ cbPlanCache .put (redisCacheKey , cbPlanList );
9398 cbPlanList .addAll (getCbPlanForAll ());
9499 } else {
95100 log .info ("Cache hit for orgId: {}, Found {} records" , orgId , cbPlanList .size ());
@@ -100,9 +105,8 @@ private List<Map<String, Object>> getCbPlanForOrgId(String orgId) {
100105
101106 public List <Map <String , Object >> getCbPlanForAllAndOrgId (String orgId , AtomicBoolean isCacheEnabled ) {
102107 List <Map <String , Object >> activeCbPlans = cbPlanCache .getIfPresent (orgId );
103- if (CollectionUtils .isEmpty (activeCbPlans )) {
108+ if (CollectionUtils .isNotEmpty (activeCbPlans )) {
104109 log .info ("Cache hit for orgId: {}, Found {} active CB Plans" , orgId , activeCbPlans .size ());
105- activeCbPlans = new ArrayList <>();
106110 return activeCbPlans ;
107111 }
108112 List <Map <String , Object >> cbPlanList = getCbPlanForOrgId (orgId );
0 commit comments