I want to implement a blocking cache for multiple requests at some time.
Currently I have this:
In file AppConfig.java
I defined the cache manager
@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;
}
}
Here is BlockingCache definition from file: 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);
}
}
I added decorator in xml config file ehache.xml
from 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>
And I want to retrive cache in method
@Cacheable(value = "findConcurrentById", key = "#id", condition = "#result != null")
public Optional<Concurrent> findBy(Long id) {
return concurrentRepo.findBy(id);
}
But still doesn't working. All I read doesn't explain more than what I already implemented. Can anyone explain me what is wrong at my code?