Skip to content

Allow us to access 'cache name' property from ReqShieldCache (or AsyncCache) #33

Open

Description

@chany714

When using Caffeine Cache, we load the Cache corresponding to a cache name from the CaffeineCacheManager and perform operations such as get and put.

Additional Question)
Should ReqShieldCache and AsyncCache have different interface names?

Activity

  1. self-assigned this
    on Jun 16, 2025
  2. pinest94 commented on Oct 1, 2025

    @pinest94

    @chany714 Is there any problem if I add the caffeine lib (in build.gradle) to the req-shield-spring-example module and write the example code?

  3. chany714 commented on Oct 1, 2025

    @chany714
    ContributorAuthor

    @chany714 Is there any problem if I add the caffeine lib (in build.gradle) to the req-shield-spring-example module and write the example code?

    @pinest94 Yes, that’s perfectly fine. The example project is just for usage examples, so adding the library is no problem.

  4. pinest94 commented on Oct 21, 2025

    @pinest94

    @chany714 I want to make sure that the sync for the direction of progress is correct.

    1. Manual configuration setup

    @Configuration
    class CaffeineCacheConfiguration {
    
        @Bean
        fun memberCache(): com.github.benmanes.caffeine.cache.Cache<String, ReqShieldData<Member>> {
            return Caffeine.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(1, TimeUnit.MINUTES)
                .build()
        }
    
        @Bean
        fun productCache(): com.github.benmanes.caffeine.cache.Cache<String, ReqShieldData<Product>> {
            return Caffeine.newBuilder()
                .maximumSize(100_000)
                .expireAfterWrite(10, TimeUnit.SECONDS)
                .build()
        }
    }
    @Configuration
    class CaffeineReqShieldBeanConfiguration {
    
        @Bean("productReqShield")
        fun productReqShield(
            @Qualifier("productCache") productCache: Cache<String, ReqShieldData<Product>>
        ): ReqShield<Product> {
            return ReqShield(
                ReqShieldConfiguration(
                    setCacheFunction = { key, value, ttl ->
                        productCache.put(key, value as ReqShieldData<Product>)
                        true
                    },
                    getCacheFunction = { key ->
                        productCache.getIfPresent(key) as? ReqShieldData<Product>
                    }
                )
            )
        }
    
        @Bean("memberReqShield")
        fun memberReqShield(
            @Qualifier("memberCache") memberCache: Cache<String, ReqShieldData<Member>>
        ): ReqShield<Member> {
            return ReqShield(
                ReqShieldConfiguration(
                    setCacheFunction = { key, value, ttl ->
                        memberCache.put(key, value as ReqShieldData<Member>)
                        true
                    },
                    getCacheFunction = { key ->
                        memberCache.getIfPresent(key) as? ReqShieldData<Member>
                    },
                )
            )
        }
    }

    I wonder if it need the context for the name in setCacheFunction, getCacheFunction in the above way.
    Would CaffeineReqShieldBeanConfiguration consider using getCache by injecting CacheManager?

  5. pinest94 commented on Oct 21, 2025

    @pinest94

    @chany714

    2. Using @ReqShieldCacheable, @ReqShieldCacheEvict annotations

    @Aspect
    @Component
    class ReqShieldAspect<T>(
        private val reqShieldCache: ReqShieldCache<T>,  // -> private val cacheManager: CacheManager
    ) : BeanFactoryAware {
    
        ... (omit) ...
    
        private val keyGeneratorMap = ConcurrentHashMap<String, KeyGenerator>()
        internal val reqShieldMap = ConcurrentHashMap<String, ReqShield<T>>()
    
        @Around("@annotation(com.linecorp.cse.reqshield.spring.annotation.ReqShieldCacheable)")
        fun aroundReqShieldCacheable(joinPoint: ProceedingJoinPoint): Any? {
            val annotation = getCacheableAnnotation(joinPoint)
            val reqShield = getOrCreateReqShield(joinPoint)
            val cacheKey = getCacheableCacheKey(joinPoint)
    
            return reqShield
                .getAndSetReqShieldData(
                    cacheKey,
                    { joinPoint.proceed() as? T },
                    annotation.timeToLiveMillis,
                ).value
        }
    
        private fun getOrCreateReqShield(joinPoint: ProceedingJoinPoint): ReqShield<T> =
            reqShieldMap.computeIfAbsent(generateReqShieldKey(joinPoint)) {
                createReqShield(joinPoint)
            }
    
        private fun createReqShield(joinPoint: ProceedingJoinPoint): ReqShield<T> {
            val annotation = getCacheableAnnotation(joinPoint)
    
            val reqShieldConfiguration =
                ReqShieldConfiguration(
                    setCacheFunction = { key, reqShieldData, timeToLiveMillis ->
                        reqShieldCache.put(key, reqShieldData, timeToLiveMillis)
                        true
                    },
                    getCacheFunction = { key ->
                        reqShieldCache.get(key)
                    },
                    globalLockFunction = { key, timeToLiveMillis ->
                        reqShieldCache.globalLock(key, timeToLiveMillis)
                    },
                    globalUnLockFunction = { key ->
                        reqShieldCache.globalUnLock(key)
                    },
                    isLocalLock = annotation.isLocalLock,
                    lockTimeoutMillis = annotation.lockTimeoutMillis,
                    decisionForUpdate = annotation.decisionForUpdate,
                    maxAttemptGetCache = annotation.maxAttemptGetCache,
                    reqShieldWorkMode = annotation.reqShieldWorkMode,
                )
    
            return ReqShield(reqShieldConfiguration)
        }

    In addition, ReqShieldAspect, the internal logic of the annotation, is being injected with the bean of the ReqShieldCache type.
    This injection method does not resolve the issue of multiple caches based on the caffeine cache name, so it seems necessary to switch to cacheManager injection.

  6. chany714 commented on Oct 21, 2025

    @chany714
    ContributorAuthor

    @pinest94
    The creation of the multiple ReqShield object raises some concerns, since it needs to manage background tasks based on multiple schedules. I’m wondering whether it would be necessary to create multiple ReqShield objects per domain.

    Also, as you mentioned in point 2, if ReqShieldAspect receives an injected CacheManager, it might lead to issues where ReqShieldCache cannot properly adhere to the interface implementation contracts.

    I think the following approach might be better. Please take a look and share your thoughts:

    • Update ReqShieldConfiguration so that both setCacheFunction and getCacheFunction can accept a name field as a parameter.
    • When using ReqShield, the user can utilize the name parameter in setCacheFunction and getCacheFunction to select and interact with the appropriate cache.

    With this approach, we can maintain a single ReqShield instance while still supporting multiple caches of different types and names.
    cc. @ykhfree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions