将第二个参数传递给番石榴缓存加载()方法



我之前问过一个关于如何在Java中实现Guava缓存的问题,见这里。 虽然它有效,但我最近注意到getAllProfile方法中的一个错误。

private LoadingCache<Integer, List<Profile>> loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(10,TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Integer, List<Profile>>() {
@Override
public List<Profile> load(Integer integer) throws Exception {
Profile profile= new Profile();
if (integer == null) {
integer = 10;
}
return profileDAO.getAllProfiles(profile, integer);
}
}
);
public List<Profile> getAllProfiles(Profile profile, Integer integer) throws Exception {
return loadingCache.get(integer);
}

在该方法中,我传入了一个名为profileProfile对象。 这样,在服务层上,用户可以使用以下@QueryParam为工作人员的配置文件设置参数,以查看他们是否仍在工作:

@GET
public List<Profile> getProfiles(@QueryParam("employed") Boolean employed, @QueryParam("size") Integer integer) {
//code for service here.  the value for query param is used in a 
//new Profile object
}

此处创建的配置文件对象通过管理器层向下传递,并传递到 DAO 层,其中设置的参数(如使用的布尔值(被解析为 select 语句的参数。

这里的问题是,自从我开始使用缓存以来,布尔值不再被解析。 使用 System.out.println 调用该方法以评估使用的字段,计算结果为 null。 这是有道理的,因为我在缓存管理器中创建了一个新的 Profile 对象,除了缓存 get 方法不在 getAllProfile 方法上获取配置文件之外,没有调用 setter;它只需要大小。

我想我可以通过在加载方法中添加一个新的配置文件参数来解决这个问题,如下所示:

private LoadingCache<Integer, List<Profile>> loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(10,TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Integer, List<Profile>>() {
@Override
public List<Profile> load(Integer integer) throws Exception {
@Override
public List<Profile> load(Integer integer, Profile profile) throws Exception {
if (integer == null) {
integer = 10;
}
return profileDAO.getAllProfiles(profile, integer);
}
}
}
);

但是,load(( 似乎只设计为接受一个参数,因此这会带来此错误:

Class 'Anonymous class derived from CacheLoader' must either be declared abstract or implement abstract method 'load(K)' in 'CacheLoader'

重申一下,我需要做的就是将在服务层中创建的配置文件对象传递给管理器层并缓存。 这似乎就像将第二个参数传递给 load(( 一样简单,但这似乎是不可能的。

编辑:

我已经编辑了getAllProfiles方法以使用Callable:

public List<Profile> getAllProfiles(Profile profile, Integer integer) throws Exception {
return loadingCache.get(size, new Callable<Profile>() {
@Override
public Profile call() throws Exception {
return profile;
}
});
}

这会产生一个错误,即我传递的是Profile而不是List<Profile>。 不过,我需要传入profile,这样我就可以解析 DAO 中的字段以获取 SQL 语句。

下面是一个示例:

public class ImageCache2 extends CaffeineCache<URL, Image> {
ImageCache2() {
this.cache = Caffeine.newBuilder()
.maximumSize(300)
.expireAfterWrite(5, TimeUnit.MINUTES)
.refreshAfterWrite(1, TimeUnit.MINUTES)
.build((k) -> null);
}
}

只需给 build 一个 null 返回,因为我们不使用它。

public static Image LoadImageFromURL(URL url, double w, double h) {
URLConnection conn;
Image returnImage;
try {
conn = url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");
try (InputStream stream = conn.getInputStream()) {
returnImage = new Image(stream, w, h, true, true);
} catch (IOException e2) {
e2.printStackTrace();
return null;
}
return returnImage;
}

这是我真正用来获取该项目的代码。

public static void useExecutors(Runnable run) {
executorServices.execute(run);
}
public void LoadImage(URL url, double w, double h, Consumer<Image> callWhenFinish) {
useExecutors(() ->
{
Image thumbImage = ImageCacheInstance.Cache().get(url, (u) -> LoadImageFromURL(url, w, h));
Platform.runLater(() ->
{
callWhenFinish.accept(thumbImage);
System.out.println("ImageLoad >> Finish -- " + this);
});
});
}

这是我调用缓存获取方法的地方。PS:useExecutors在后台线程中运行它

最新更新