Spring@Cacheable不';行不通

Spring@Cacheable不';行不通,spring,ehcache,spring-cache,Spring,Ehcache,Spring Cache,我有一个具有以下方法的服务: 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); }

我有一个具有以下方法的服务:

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);
}
public可选getTestWithId100(){
返回get(100);
}
@可缓存(value=“test”,key=“'1'))
公共可选get(长id){
log.error(“不是来自缓存”);
返回testRepository.findOneById(id);
}
我从控制器调用getTestWithId100方法,但它只获得新值

@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;
    }

}
@Slf4j
@配置
@启用缓存
@AutoConfigureAfter(值={MetricsConfiguration.class,DatabaseConfiguration.class})
公共类缓存配置{
@持久上下文
私人实体管理者实体管理者;
私人最终计量注册;
private net.sf.ehcache.CacheManager CacheManager;
@注入
公共缓存配置(MetricRegistry MetricRegistry){
this.metricRegistry=metricRegistry;
}
@发情前期
公共空间销毁(){
log.info(“删除缓存管理器指标”);
SortedSet name=metricRegistry.getNames();
name.forEach(metricRegistry::remove);
log.info(“关闭缓存管理器”);
cacheManager.shutdown();
}
@豆子
公共缓存管理器缓存管理器(属性){
调试(“启动Ehcache”);
cacheManager=net.sf.ehcache.cacheManager.create();
cacheManager.getConfiguration().setMaxBytesLocalHeap(properties.getCache().getEhcache().getMaxBytesLocalHeap());
log.debug(“注册Ehcache度量”);
entityManager.getMetamodel().getEntities().forEach(实体->{
字符串名称=entity.getName();
if(name==null | | entity.getJavaType()!=null)
name=entity.getJavaType().getName();
Assert.notNull(名称,“没有标识符的实体不能存在”);
net.sf.ehcache.Cache Cache=cacheManager.getCache(名称);
if(缓存!=null)
cacheManager.replaceCacheWithDecoratedCache(cache,InstrumentedEhcache.instrument(metricRegistry,cache));
});
ehCacheManager ehCacheManager=新的ehCacheManager();
ehCacheManager.setCacheManager(cacheManager);
返回管理器;
}
}
ehcache.xml的一部分:

<cache name="test" eternal="true"/>


为什么它不起作用?我尝试了不同的键,但没有成功。

Spring注释通过代理/增强类来工作。该系统的一个限制是,当您在同一个bean上调用一个方法时,系统不会拦截该调用,因此不会应用基于注释的修改。

Spring注释通过代理/增强类来工作。这个系统中的一个限制是当您在同一bean上调用方法时,该调用不会被系统拦截,因此不会应用基于注释的修改。

< P>我认为您在<代码> Cache配置文件< /代码>中出错,让我们考虑下面的代码(它对我来说很好):

当然,
ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="true"
    monitoring="autodetect"
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />

    <cache name="movieFindCache"
        maxEntriesLocalHeap="10000"
        maxEntriesLocalDisk="1000"
        eternal="false"
        diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="300" timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>


此配置必须帮助您,如果无法解决问题,请通知我。Hth

< P>我认为你在<代码> Cache配置文件中是错误的,让我们考虑下面的代码(它对我来说很好):

当然,
ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="true"
    monitoring="autodetect"
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />

    <cache name="movieFindCache"
        maxEntriesLocalHeap="10000"
        maxEntriesLocalDisk="1000"
        eternal="false"
        diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="300" timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

此配置必须帮助您,如果无法解决问题,请通知我。HTH

请参见:请参见: