繁体   English   中英

弹簧缓存不能在重负载下工作?

[英]spring caching not working under heavy load?

我在启动应用程序中启用了弹簧缓存。 简单的测试表明它正在工作。 但是,当我对它运行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("suggestions", key="#city.toString() + #item.toString()")
public Suggestion getSuggestionFor(String city, String item) {
    ....
}

您可以从这个很好的答案中获得更多有关缓存键的问题的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM