Guava LoadingCache无法在SpringBoot应用程序中运行?

时间:2017-01-13 20:17:52

标签: java spring caching guava

我定义了一个Guava LoadingCache,它接受搜索条件请求。搜索条件类正确实现了equalshashCode,并通过单元测试进行了验证。

缓存正常工作并在单元测试中缓存搜索结果,但在作为SpringBoot应用程序运行时,缓存不起作用,并且每次调用load时都调用get(key)

我会发布我的代码但是我通过日志记录和测试来变化,除了作为SpringBoot胖jar运行时,缓存还可以工作。

是否有一些我不了解Spring会引起这种情况的事情?

编辑: 包含LoadingCache的类:

public class ObjectCache {

private static ObjectCache objectCache = null;
LoadingCache<Key, Object> serviceCache;

private ObjectCache(){
    System.out.println("creating cache");
    serviceCache = CacheBuilder.newBuilder()
            .expireAfterWrite(20, TimeUnit.MINUTES)   
            .maximumSize(1000)
            .build(new CacheLoader<Key, Object>() {
                public Object load(Key key) throws Exception {
                    System.out.println("uncached request - calling legacy service: " + key.hashCode());
                    return ServiceCaller.callLegacyFindPatient(key);
                }
            });
}

public static ObjectCache getObjectCache(){
    if(_patientCache == null){
        _patientCache = new FindPatientCache();
    }
    return _patientCache;
}

public LoadingCache<Key, Object> getCache(){
    return this.serviceCache;
}

}

缓存的使用方式如下:ObjectCache.getPatientCache().getCache().get(key);

在单元测试中,缓存有效,我看到了重复的哈希值。当作为SpringBoot应用程序运行时,相同的调用会导致缓存每次调用旧的Web服务,即使哈希显示为相同。

编辑:调用SpringBoot应用程序时控制台输出,您可以看到相同的哈希值触发负载。

creating cache
uncached request - calling native service for key: 2076189345
uncached request - calling native service for key: 2076189345

0 个答案:

没有答案