Java 如何在Spring中使用Ehcache中的BlockingCache?
我想在某个时候为多个请求实现一个阻塞缓存 目前我有: 在文件Java 如何在Spring中使用Ehcache中的BlockingCache?,java,spring,ehcache,ehcache-2,Java,Spring,Ehcache,Ehcache 2,我想在某个时候为多个请求实现一个阻塞缓存 目前我有: 在文件AppConfig.java中,我定义了缓存管理器 @EnableCaching @Configuration public class AppConfig implements WebMvcConfigurer { @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(Objects.requireNonNu
AppConfig.java
中,我定义了缓存管理器
@EnableCaching
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(Objects.requireNonNull(ehCacheCacheManager().getObject()));
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
}
以下是文件中的BlockingCache定义:BlockingCachedDecoratorFactory.java
@Component
public class BlockingCacheDecoratorFactory extends CacheDecoratorFactory {
public Ehcache createDecoratedEhcache(final Ehcache cache, final Properties properties) {
return new BlockingCache(cache);
}
public Ehcache createDefaultDecoratedEhcache(final Ehcache cache, final Properties properties) {
return new BlockingCache(cache);
}
}
我从resources
xml配置文件ehache.xml
中添加了decorator
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<cache name="findConcurrentById"
eternal="false"
maxEntriesLocalHeap="20000"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU">
<cacheDecoratorFactory class="com.site.configuration.BlockingCacheDecoratorFactory"/>
</cache>
</ehcache>
我想在方法中检索缓存
@Cacheable(value = "findConcurrentById", key = "#id", condition = "#result != null")
public Optional<Concurrent> findBy(Long id) {
return concurrentRepo.findBy(id);
}
@Cacheable(value=“findConcurrentById”,key=“#id”,condition=“#result!=null”)
公共可选findBy(长id){
返回concurrentRepo.findBy(id);
}
但还是不行。
我所读到的只是解释了我已经实现的东西。
有人能解释一下我的代码出了什么问题吗