我有下一个方法服务:春天@Cacheable不起作用
public Optional<Test> getTestWithId100() {
return get(100);
}
@Cacheable(value = "test", key = "'1'")
public Optional<Test> get(long id) {
log.error("not from cache");
return testRepository.findOneById(id);
}
我打电话方法getTestWithId100从控制器,但只得到新鲜的价值。 ehcache.xml中的
@Slf4j
@Configuration
@EnableCaching
@AutoConfigureAfter(value = { MetricsConfiguration.class, DatabaseConfiguration.class })
public class CacheConfiguration {
@PersistenceContext
private EntityManager entityManager;
private final MetricRegistry metricRegistry;
private net.sf.ehcache.CacheManager cacheManager;
@Inject
public CacheConfiguration(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
}
@PreDestroy
public void destroy() {
log.info("Remove Cache Manager metrics");
SortedSet<String> names = metricRegistry.getNames();
names.forEach(metricRegistry::remove);
log.info("Closing Cache Manager");
cacheManager.shutdown();
}
@Bean
public CacheManager cacheManager(Properties properties) {
log.debug("Starting Ehcache");
cacheManager = net.sf.ehcache.CacheManager.create();
cacheManager.getConfiguration().setMaxBytesLocalHeap(properties.getCache().getEhcache().getMaxBytesLocalHeap());
log.debug("Registering Ehcache Metrics gauges");
entityManager.getMetamodel().getEntities().forEach(entity -> {
String name = entity.getName();
if (name == null || entity.getJavaType() != null)
name = entity.getJavaType().getName();
Assert.notNull(name, "entity cannot exist without a identifier");
net.sf.ehcache.Cache cache = cacheManager.getCache(name);
if (cache != null)
cacheManager.replaceCacheWithDecoratedCache(cache, InstrumentedEhcache.instrument(metricRegistry, cache));
});
EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
ehCacheManager.setCacheManager(cacheManager);
return ehCacheManager;
}
}
部分:
<cache name="test" eternal="true"/>
为什么它不工作?我尝试了不同的钥匙,但没有成功。
请参阅:http://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s – rpozarickij