I'm working with spring based application (not spring boot) and I'm trying to introduce cache2k as a spring cache manager.
Currently cache2k is used as a Hibernate second level cache by setting the following properties
hibernate.cache.use_second_level_cache = true
hibernate.cache.region.factory_class = org.hibernate.cache.jcache.JCacheRegionFactory
hibernate.javax.cache.provider = org.cache2k.jcache.provider.JCacheProvider
with the following cache2k.xml configuration file (only the relevant part related to the example)
<cache2k xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='https://cache2k.org/schema/v1.x'
xsi:schemaLocation="https://cache2k.org/schema/v1.x https://cache2k.org/schema/cache2k-core-v1.x.xsd">
<!-- The configuration example for the documentation -->
<version>1.0</version>
<skipCheckOnStartup>true</skipCheckOnStartup>
<ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>
<defaults>
<cache>
<entryCapacity>100</entryCapacity>
</cache>
</defaults>
<templates>
<cache>
<name>lookupTable</name>
<expireAfterWrite>1d</expireAfterWrite>
<entryCapacity>50_000</entryCapacity>
</cache>
</templates>
<caches>
<cache>
<name>AdvertisingCategoryCache</name>
<include>lookupTable</include>
</cache>
</caches>
Now I want to introduce a Spring cache manager and in the cache2k documentation there're enough information in order to introduce it as a spring cache manager also with the following code
@EnableCaching
...
...
@Bean
public CacheManager cacheManager() {
return new SpringCache2kCacheManager();
}
The final goal is to reuse the existing hibernate second level cache also in the spring caching layer when needed but when calling the following method
@Cacheable(cacheNames = "AdvertisingCategoryCache")
@Override
public AdvertisingCategory findById(Long id) {
// call to the repository
}
I get the following error
Cache already created: 'AdvertisingCategoryCache'
The point is that I know that the cache is already created. My need is to reuse the existing one.
How can I do that?
P.S. The example is very easy and for sure I can directly remove the @Cacheable method, but I've used that in order to provide an easy explanation of my situation.