我在启动应用中启用了弹簧缓存。简单的测试表明它正在工作。但是,当我对它运行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;
}
答案 0 :(得分:0)
我不确定这是一个很重的问题,可能只是因为你还没有指定关键是城市和项目的组合。尝试这样的声明。
@Cacheable("suggestions", key="#city.toString() + #item.toString()")
public Suggestion getSuggestionFor(String city, String item) {
....
}
您可以在this great answer上阅读与缓存密钥相关的问题的更多信息。