I have created a Springboot app and have implemented in-memory Cache & throttling using Caffeine and bucket4j. application.yml
spring:
cache:
jcache:
provider: com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider
cache-names:
- allEndpoints
- hello
- world
- myCache1
- myCache2
caffeine:
spec: maximumSize=1000000,expireAfterAccess=3600s
bucket4j:
enabled: true
filters:
- cache-name: allEndpoints
url: /api/v1/.*
rate-limits:
- bandwidths:
- capacity: 10
time: 1
unit: minutes
fixed-refill-interval: 1
fixed-refill-interval-unit: minutes
- cache-name: hello
url: /api/v1/hello.*
rate-limits:
- bandwidths:
- capacity: 2
time: 1
unit: minutes
fixed-refill-interval: 1
fixed-refill-interval-unit: minutes
- cache-name: world
url: /api/v1/world.*
rate-limits:
- bandwidths:
- capacity: 2
time: 1
unit: minutes
fixed-refill-interval: 1
fixed-refill-interval-unit: minutes
The caches allEndpoints
, hello
and world
are used by bucket4j
. Whereas the caches myCache1
and myCache2
are used by application to cache the results returned by the service methods. Below is the service class.
@Service
public class TestService {
@Cacheable("myCache1")
public String myService1(int key){
System.out.println("Service1 Invoked");
return "hello from Service1:: " + key;
}
@Cacheable("myCache2")
public String myService2(int key){
System.out.println("Service2 Invoked");
return "hello from Service2:: " + key;
}
}
I would like to know if it is possible to have multiple expireAfterAccess
or expireAfterWrite
for the memory caches the application uses. i.e. something like mentioned in this article. TIA.
The entire code can be found here.