I want to set up caching in Spring Boot using EhCache 3 and Spring Caching. How do I set up the cache creation? Under what circumstances is the customize()
method of JCacheManagerCustomizer
called?
The cache is for Spring Security ACLs. At the end I need an AclCache.
I got these beans configured in the application class.
@Bean
public AclCache aclCache(Cache cache, PermissionGrantingStrategy permissionGrantingStrategy, AclAuthorizationStrategy aclAuthorizationStrategy) {
return new SpringCacheBasedAclCache(cache, permissionGrantingStrategy, aclAuthorizationStrategy);
}
@Bean
public LookupStrategy lookupStrategy(DataSource dataSource, AclCache aclCache, AclAuthorizationStrategy aclAuthorizationStrategy, PermissionGrantingStrategy permissionGrantingStrategy) {
return new BasicLookupStrategy(dataSource, aclCache, aclAuthorizationStrategy, permissionGrantingStrategy);
}
@Bean
public JdbcMutableAclService jdbcMutableAclService(DataSource dataSource, LookupStrategy lookupStrategy, AclCache aclCache) {
return new JdbcMutableAclService(dataSource, lookupStrategy, aclCache);
}
In a seperate class I got these beans:
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public CacheManager cacheManager() {
return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager());
}
@Bean
public Cache cache(CacheManager cacheManager, JCacheManagerCustomizer cacheManagerCustomizer) {
Cache cache = cacheManager.getCache("aclCache");
return cache;
}
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
return new JCacheManagerCustomizer() {
@Override
public void customize(javax.cache.CacheManager cacheManager) {
org.ehcache.config.CacheConfiguration<Object, Object> config = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES))
.withExpiry(ExpiryPolicy.NO_EXPIRY)
.build();
cacheManager.createCache("aclCache", Eh107Configuration.fromEhcacheCacheConfiguration(config));
}
};
}
}
Now the cache aclCache
should be created, but isn't. According to this example [1] it should be. Placing a breakpoint in the customize()
method shows, that it is not hit. The JCacheManagerCustomizer is called though.