I am trying to implement bucket4j and it is working on a default non customized cache (jCache I guess).
spring:
...
cache:
cache-names: rate_limit_buckets
bucket4j:
enabled: true
filters:
- cache-name: rate_limit_bucketss
url: /api/notification-admin/images
rate-limits:
- cache-key: getRemoteAddr()
bandwidths:
- capacity: 1
time: 100
unit: seconds
However when I try to use caffeine,
cache:
type: caffeine
cache-names: rate_limit_buckets
the app starts, but the rate limiting does not work. It is even weirder that I can set cache-name to anything and the app starts. (If I do not use caffeine the app fails to start.)
I tried different ways to config caffeine (in a class, coffee-boots) but the results are the same, usually it does not work at all or it seems to ignore the caffeine config.
Thank you for your tips.
As I understand it, Bucket4j uses
java.cache.CacheManager
to get the cache and if we specify thecache.type=caffeine
we are creating the defaultorg.springframework.cache.CacheManager
and cannot be foundjava.cache.CacheManager
. I had a problem creating multiple caffeine caches with different specifications. I managed to solve this problem by creating multiple CacheManager in the configuration class:javax.cache.CacheManager
(com.github.benmanes.caffeine.jcache.CacheManagerImpl) used to create a cache for Bucket4j:org.springframework.cache.CacheManager
(org.springframework.cache.caffeine implementation.CaffeineCacheManager) for creating a caffeine caches:We use
@Primary
to give higher preference to a bean when there are multiple beans of the same type.You also need to add an attribute with the necessary CacheManager to the
@Cacheable
annotation: