我想使用 EhCache 3 和 Spring Caching 在 Spring Boot 中设置缓存。如何设置缓存创建?什么情况下调用的customize()
方法JCacheManagerCustomizer
?
缓存用于 Spring Security ACL。最后我需要一个 AclCache。
我在应用程序类中配置了这些 bean。
@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);
}
在一个单独的课程中,我得到了这些豆子:
@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));
}
};
}
}
aclCache
现在应该创建缓存,但不是。根据这个例子 [1] 它应该是。在方法中放置一个断点customize()
表明它没有被命中。虽然调用了 JCacheManagerCustomizer。