Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@cache不适用于无参数的缓存列表_Java_Spring_Caching - Fatal编程技术网

Java Spring@cache不适用于无参数的缓存列表

Java Spring@cache不适用于无参数的缓存列表,java,spring,caching,Java,Spring,Caching,您好,我在尝试缓存列表函数的结果时遇到问题 当我第一次调试它时,第二次调用该函数时,缓存不返回任何内容,而是返回一个空列表 我还尝试添加key=“'all'”和方法名,但没有任何结果 @Override @Cacheable(value = "categories" ) public List<Category> getCategories() { // TODO Auto-generated method stub LOG.info("start getCatego

您好,我在尝试缓存列表函数的结果时遇到问题 当我第一次调试它时,第二次调用该函数时,缓存不返回任何内容,而是返回一个空列表 我还尝试添加key=“'all'”和方法名,但没有任何结果

@Override
@Cacheable(value = "categories" )
public List<Category> getCategories() {
    // TODO Auto-generated method stub
    LOG.info("start getCategories");
    return repository.findAll(); //
}
@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
@Bean(destroyMethod="shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
     net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();



    CacheConfiguration cacheConfigurationCategories = new CacheConfiguration();
    cacheConfigurationCategories.setName("categories");

    cacheConfigurationCategories.setMemoryStoreEvictionPolicy("LRU");
    cacheConfigurationCategories.setMaxEntriesLocalHeap(100);
    cacheConfigurationCategories.setMaxElementsOnDisk(100);
    config.addCache(cacheConfigurationCategories);

    return net.sf.ehcache.CacheManager.newInstance(config);
}

@Bean
@Override
public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheManager());
}

@Bean
@Override
public KeyGenerator keyGenerator() {
    return new SimpleKeyGenerator();
}
@覆盖
@可缓存(value=“categories”)
公共列表getCategories(){
//TODO自动生成的方法存根
LOG.info(“开始获取类别”);
返回repository.findAll()//
}
@配置
@启用缓存
公共类CachingConfig实现cachingconfiguer{
@Bean(destromethod=“shutdown”)
public net.sf.ehcache.CacheManager ehCacheManager(){
net.sf.ehcache.config.Configuration config=new net.sf.ehcache.config.Configuration();
cacheConfigurationCategories=新的CacheConfiguration();
cacheConfigurationCategories.setName(“类别”);
cacheConfigurationCategories.SetMemoryStoreReceivingPolicy(“LRU”);
cacheConfigurationCategories.setMaxEntriesLocalHeap(100);
cacheConfigurationCategories.setMaxElementsOnDisk(100);
config.addCache(cacheConfigurationCategories);
返回net.sf.ehcache.CacheManager.newInstance(配置);
}
@豆子
@凌驾
公共缓存管理器缓存管理器(){
返回新的ehCacheManager(ehCacheManager());
}
@豆子
@凌驾
公钥生成器KeyGenerator(){
返回新的SimpleKeyGenerator();
}

}

我只在xml中的配置中使用过它,如下所示

    @Bean(name = "ehcache")
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        return ehCacheManagerFactoryBean;
    }

    @Bean
    public CacheManager cacheManager() {
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
        cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
        return cacheManager;
    }
使用以下xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>

<cache name="cacheName"
       maxElementsInMemory="1000"
       eternal="false"
       timeToIdleSeconds="28800"
       timeToLiveSeconds="28800"
       overflowToDisk="false"
       maxElementsOnDisk="200"
       diskPersistent="false"
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="LRU"/>             

</ehcache>


代码看起来正常,因此repository.findAll不会返回任何内容,或者您的缓存配置错误。您好,添加了我的缓存配置。您可以放置您的xml吗?很难说有什么问题。尝试在可缓存注释中添加一个键。它适用于单个元素,但不适用于对象列表,我尝试了键,但不知道还能做什么。打开日志记录并尝试最简单的情况。