11package com .igot .cb .cache ;
22
3+ import java .time .Duration ;
34import java .util .*;
45import java .util .concurrent .ConcurrentHashMap ;
56import java .util .stream .Collectors ;
67
8+ import com .github .benmanes .caffeine .cache .Cache ;
9+ import com .github .benmanes .caffeine .cache .Caffeine ;
710import com .igot .cb .cassandra .CassandraOperation ;
11+ import jakarta .annotation .PostConstruct ;
812import org .apache .commons .collections .CollectionUtils ;
913import org .apache .commons .collections .MapUtils ;
14+ import org .springframework .beans .factory .annotation .Value ;
1015import org .springframework .stereotype .Component ;
1116
1217import com .igot .cb .model .CachedAccessSettingRule ;
@@ -25,21 +30,38 @@ public class AccessSettingRuleCacheMgr {
2530 private final CassandraOperation cassandraOperation ;
2631 private Map <String , CachedAccessSettingRule > cachedAccessSettingRules = new ConcurrentHashMap <>();
2732
33+ private Cache <String , CachedAccessSettingRule > accessSettingsCache ;
34+
35+
2836 private final long LOCAL_CACHE_TTL = 3600000 ;
2937
3038 private final String ACCESS_SETTINGS_CACHE_KEY = "accessSettingRules" ;
3139
40+ @ Value ("${access.rule.ttl.minutes}" )
41+ private int ttlMinutes ;
42+
43+ @ PostConstruct
44+ public void initCache () {
45+ accessSettingsCache = Caffeine .newBuilder ()
46+ .maximumSize (1000 )
47+ .expireAfterWrite (Duration .ofMinutes (ttlMinutes ))
48+ .build ();
49+ }
50+
51+
3252 /**
3353 * Constructor for AccessSettingRuleCacheMgr.
3454 *
3555 * @param redisCacheMgr Cache manager for Redis operations.
3656 * @param cassandraOperation Cassandra operations for database interactions.
3757 */
38- public AccessSettingRuleCacheMgr (RedisCacheMgr redisCacheMgr , CassandraOperation cassandraOperation ) {
58+ public AccessSettingRuleCacheMgr (RedisCacheMgr redisCacheMgr ,
59+ CassandraOperation cassandraOperation ) {
3960 this .redisCacheMgr = redisCacheMgr ;
4061 this .cassandraOperation = cassandraOperation ;
4162 }
4263
64+
4365 /**
4466 * Retrieves the cached access setting rules.
4567 * If the cache is empty or expired, it loads the rules from Redis or Cassandra.
@@ -207,17 +229,12 @@ BitSet createBitSetForAttribute(Collection<Integer> attributeValues) {
207229 return bitSet ;
208230 }
209231
210-
211232 public CachedAccessSettingRule getOrLoadAccessSettingRule (String courseId , String contextId ) {
212-
213233 String cacheKey = courseId + "|" + contextId ;
214- if (MapUtils .isEmpty (cachedAccessSettingRules )) {
215- cachedAccessSettingRules = new ConcurrentHashMap <>();
216- }
217- CachedAccessSettingRule rule = cachedAccessSettingRules .get (cacheKey );
218- if (rule != null ) {
234+ CachedAccessSettingRule cachedRule = accessSettingsCache .getIfPresent (cacheKey );
235+ if (cachedRule != null ) {
219236 log .debug ("Cache hit for rule key: {}" , cacheKey );
220- return rule ;
237+ return cachedRule ;
221238 }
222239 log .info ("Cache miss for rule key: {}, loading from Cassandra..." , cacheKey );
223240 try {
@@ -248,7 +265,7 @@ public CachedAccessSettingRule getOrLoadAccessSettingRule(String courseId, Strin
248265 if (MapUtils .isNotEmpty (contextData )) {
249266 processContextData (cacheKey , contextData );
250267 }
251- cachedAccessSettingRules .put (cacheKey , loadedRule );
268+ accessSettingsCache .put (cacheKey , loadedRule );
252269 log .info ("Loaded and cached rule for key: {}" , cacheKey );
253270 } catch (Exception e ) {
254271 log .error ("Error processing rule {}: {}" , cacheKey , e .getMessage (), e );
0 commit comments