Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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缓存在重载下不工作?_Java_Spring_Caching - Fatal编程技术网

Java spring缓存在重载下不工作?

Java spring缓存在重载下不工作?,java,spring,caching,Java,Spring,Caching,我在启动应用程序中启用了spring缓存。简单的测试表明它是有效的。然而,当我对它运行jmeter时,我看到输出非常清楚地指示缓存未命中(以毫秒为单位) 最终,它会安静下来,但不会在它出错之前,因为它会使我的数据库连接池过载 当我再次运行相同的测试时,它工作得非常好;缓存已被初始化。有人能解释一下吗 样本输出 Looking for 'Philadelphia', 'Shoe' at timestamp: 1454203396865 Looking for 'Philadelphia', 'Sh

我在启动应用程序中启用了spring缓存。简单的测试表明它是有效的。然而,当我对它运行jmeter时,我看到输出非常清楚地指示缓存未命中(以毫秒为单位)

最终,它会安静下来,但不会在它出错之前,因为它会使我的数据库连接池过载

当我再次运行相同的测试时,它工作得非常好;缓存已被初始化。有人能解释一下吗

样本输出

Looking for 'Philadelphia', 'Shoe' at timestamp: 1454203396865
Looking for 'Philadelphia', 'Shoe' at timestamp: 1454203396863
Looking for 'Atlanta', 'Wii' at timestamp: 1454203396992
Looking for 'Atlanta', 'Wii' at timestamp: 1454203397001
Looking for 'Atlanta', 'Wii' at timestamp: 1454203396998
Looking for 'Atlanta', 'Wii' at timestamp: 1454203396998
Looking for 'Atlanta', 'Wii' at timestamp: 1454203396993
Looking for 'San Francisco', 'Jeep' at timestamp: 1454203397174
Looking for 'San Francisco', 'Jeep' at timestamp: 1454203397187
Looking for 'San Francisco', 'Jeep' at timestamp: 1454203397190

Starting the test @ Sat Jan 30 18:41:55 PST 2016 (1454208115431)
Waiting for possible shutdown message on port 4445
summary +      1 in   7.1s =    0.1/s Avg:  2022 Min:  2022 Max:  2022 Err:     1 (100.00%) Active: 100 Started: 100 Finished: 0
summary +   7999 in  17.3s =  463.4/s Avg:   244 Min:    82 Max:  9965 Err:    43 (0.54%) Active: 0 Started: 100 Finished: 100
summary =   8000 in  24.3s =  328.9/s Avg:   244 Min:    82 Max:  9965 Err:    44 (0.55%)
Tidying up ...    @ Sat Jan 30 18:42:19 PST 2016 (1454208139824)
... end of run
Violas-MacBook-Pro:ou cbongiorno$ jmeter -n -t src/test/test-plan.jmx -l mc_results.jtl
Creating summariser <summary>
Created the tree successfully 
Starting the test @ Sat Jan 30 18:42:24 PST 2016 (1454208144475)
Waiting for possible shutdown message on port 4445
summary +   3034 in   5.5s =  554.8/s Avg:    93 Min:    81 Max:   272 Err:     0 (0.00%) Active: 100 Started: 100 Finished: 0
summary +   4966 in   7.1s =  702.1/s Avg:    91 Min:    81 Max:   325 Err:     0 (0.00%) Active: 0 Started: 100 Finished: 100
summary =   8000 in    13s =  637.9/s Avg:    92 Min:    81 Max:   325 Err:     0 (0.00%)
Tidying up ...    @ Sat Jan 30 18:42:37 PST 2016 (1454208157073)
@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
@EnableScheduling
@EnableCaching
public class SampleApp {

    public static void main(String[] args) throws Exception {
        System.setProperty("hibernate.temp.use_jdbc_metadata_defaults","false");

        SpringApplication.run(SampleApp.class, args);
    }
}
不在同一个班

   @Cacheable("suggestions")
    public Suggestion getSuggestionFor(String city, String item) {
        System.out.format("Looking for '%s', '%s' at timestamp: %s\r\n", city,item,System.currentTimeMillis());
        Suggestion result = null;
        List<Offer> offers = null;
        if(city != null)
            offers = repository.find(city, item);
        else
            offers = repository.find(item);

        if (offers != null && offers.size() > 0) {
            Map<Integer, Long> byPrice = offers.stream().collect(groupingBy(Offer::getListPrice, TreeMap::new, counting()));

            Map.Entry<Integer, Long> entry = byPrice.entrySet().stream().reduce((e1, e2) -> MODE.compare(e1, e2) > 0 ? e1 : e2).orElse(null);
            if (entry != null)
                result = new Suggestion(city, item, entry.getKey(), entry.getValue(), offers.size());
        }
        return result;
    }
@Cacheable(“建议”)
公共建议getSuggestionFor(字符串城市,字符串项目){
System.out.format(“在时间戳:%s处查找'%s','%s',城市,项目,系统.currentTimeMillis());
建议结果=空;
列表报价=空;
如果(城市!=null)
offers=repository.find(城市、项目);
其他的
offers=repository.find(项目);
if(offers!=null&&offers.size()>0){
Map byPrice=offers.stream().collect(groupingBy(Offer::getListPrice,TreeMap::new,counting());
Map.Entry-Entry=byPrice.entrySet().stream().reduce((e1,e2)->MODE.compare(e1,e2)>0?e1:e2)。orElse(null);
if(条目!=null)
结果=新建议(城市、项目、entry.getKey()、entry.getValue()、offers.size());
}
返回结果;
}

我不确定这是否是一个重负载的问题,可能只是因为您没有将密钥指定为城市和项目的组合。尝试这样的声明

@Cacheable("suggestions", key="#city.toString() + #item.toString()")
public Suggestion getSuggestionFor(String city, String item) {
    ....
}

您可以从中阅读有关缓存键的问题的更多信息。

我假设它只使用参数列表中所有对象的hashcode/equals。我按照您的建议做了,现在它抱怨在那些参数为null时调用toString()。我将指出,您实际上不必使用toString(),甚至可以包含null值(如果它们可能为null)
#city?.toString()
可能会在我的头顶上工作,以解决npe问题。没有骰子:相同的结果。但是“?”为nullsWhat调用了
getSuggestionFor
?同一个豆子里面什么都没有,对吧?请记住,只有通过spring生成的代理类传入的外部方法调用才会被拦截并命中缓存。不,controller->service.getSuggestionFor(…)一旦我再次运行测试,或者如果我在本地运行它并戳它,它就可以完美地工作。这就是为什么ti看起来是一个高速的东西?也许是因为4.3版本的快速计算?据maven central称,最新的spring版本是4.2.4——这当然有潜力。在哪里可以找到4.3版本?在Linux上尝试与mac完全不同的缓存行为。狂野的。