Reputation: 225
I am trying to setup caffeine cache using spring xml bean configuration. I want to have two different caches,
I tried doing following,
<bean id="cacheManager" class="org.springframework.cache.caffeine.CaffeineCacheManager">
<property name="cacheNames">
<set>
<value>id</value>
<value>name</value>
</set>
</property>
<property name="cacheSpecification" value="${caffeine.spec}"/>
</bean>
Code where I am using it looks like,
@Cacheable(cacheNames = {"id"})
public String getId(final String key){
System.out.println("no id in cache");
//code
}
@Cacheable(cacheNames = {"name"})
public String getName(final String key){
System.out.println("no name in cache");
//code
}
The getId()
method somehow works as per the caffeine.spec
values which is maximumSize=500,expireAfterAccess=5s
in my project. So if I call the method within 5 sec it does not print the message and if I call it within 5sec it calls the method. But the getName
does not work. It prints the message all the time.
Anyone has ever tried to setup caffeine cache to setup multiple caches.
Just a note for people looking for an answer for above issue, Looks like the above configuration actually works, it must have been some other issue which did not work for me at that time.
Upvotes: 2
Views: 1884
Reputation: 86
I had similar issue with cache configuration. Appeared that there was another cache provider in the class-path (Guava) which has been chosen by Spring instead of Caffeine.
You have to specify which cache provider is the default one by using spring.cache.type=caffeine property. However you have solved this with configuration bean.
Hope that will save some time to other people.
Upvotes: 2