0

我想使用 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。

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

4

1 回答 1

0

您正在自己配置所有内容,因此您有效地绕过了自动配置(Spring Boot 不会做任何事情,因为您已经提供了自己的配置)。

目前还不清楚你对那些Cache暴露为 bean 的东西做了什么,因为它们不会被你的缓存管理器拾取。无论如何,只需从创建的第一个方法调用该代码,CacheManager因为您是自己创建的。

于 2019-01-29T21:36:46.933 回答