我想在某个时间为多个请求实现阻塞缓存。
目前我有这个:
在文件中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 定义:BlockingCacheDecoratorFactory.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);
}
}
我在 xml 配置文件中添加了装饰ehache.xml
器resources
<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);
}
但仍然无法正常工作。我所读到的并没有比我已经实现的解释更多。谁能解释我的代码有什么问题?