I have a simple sprint boot application using spring boot 1.5.11.RELEASE
with @EnableCaching
on the Application Configuration
class.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
application.properties
spring.cache.type=caffeine
spring.cache.cache-names=cache-a,cache-b
spring.cache.caffeine.spec=maximumSize=100, expireAfterWrite=1d
Question
My question is simple, how can one specify a different size/expiration per cache. E.g. perhaps it's acceptable for cache-a
to be valid for 1 day
. But cache-b
might be ok for 1 week
. The specification on a caffeine cache appears to be global to the CacheManager
rather than Cache
. Am I missing something? Perhaps there is a more suitable provider for my use case?
I converted my initial PR into a separate tiny project.
To start using it just add the latest dependency from Maven Central:
Format of properties is the following:
If no specific configuration is defined,
CacheManager
defaults to Spring's behavior.This is your only chance:
Just expose your custom caches as beans. They are automatically added to the
CaffeineCacheManager
.I config multiple cache manager like this
And use the cache normally
Update
It look like the above code has a big mistake. So I change to
This
AppCacheConfig
class allow you to define many cache spec as you prefer. And you can define cache spec in application.yml fileBut still, this class has a limitation that we can only set
timeout
andmax
size only. because ofCacheSpec
classTherefore, If you like to add more config parameters, you are to add more parameters on
CacheSpec
class and set theCache
configuration onAppCacheConfig.buildCache
function.Hope this help!