我编写了一个程序来使用 Google Guava 缓存对象。我的问题是如何将附加参数传递给 Guava Load 方法。这是代码。下面你在这一行中看到 - 我已将 fileId 和 pageno 作为键 - cache.get(fileID+pageNo);。现在,当 cache.get 被调用并且该键不在缓存中时 - 番石榴将调用我在下面给出的类 PreviewCacheLoader 的加载方法。
public class PreviewCache {
static final LoadingCache<String, CoreObject> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(new PreviewCacheLoader());
public CoreObject getPreview(String strTempPath, int pageNo, int requiredHeight, String fileID, String strFileExt, String ssoId) throws IOException
{
CoreObject coreObject = null;
try {
coreObject = cache.get(fileID+pageNo, HOW TO PASS pageNO and requiredHeight because I want to keep key as ONLY fileID+pageNo );
} catch (ExecutionException e) {
e.printStackTrace();
}
return coreObject;
}
}
除了关键参数之外,如何将上面的 int 和 String 参数传递给下面的 Load 方法
public class PreviewCacheLoader extends CacheLoader<String, CoreObject> {
@Override
public CoreObject load(String fileIDpageNo, HOW TO GET pageNO and requiredHeight) throws Exception {
CoreObject coreObject = new CoreObject();
// MAKE USE OF PARAMETERS pageNO and requiredHeight
// Populate coreObject here
return coreObject;
}
}