0

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.

[1] https://github.com/spring-petclinic/spring-petclinic-reactjs/blob/master/src/main/java/org/springframework/samples/petclinic/config/CacheConfig.java

Daniel
  • 458
  • 5
  • 16

1 Answers1

0

You are configuring everything yourself so you are effectively bypassing the auto-configuration (Spring Boot will not do anything since you've provided your own config).

It's unclear what you're doing with those Cache exposed as beans as they aren't going to be picked up by your cache manager. Regardless of that, just invoke that code from the first method that creates the CacheManager since you are creating it yourself.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89