我之前在这里问了一个关于如何用Java实现Guava Cache的问题,见here。虽然它有效,但我最近发现了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);
}
在方法中,我传入一个名为Profile
的{{1}}对象。这样,在服务层上,用户可以使用@QueryParam为工作人员的配置文件设置参数,以查看他们是否仍在使用:
profile
此处创建的配置文件对象通过管理器层传递到DAO层,其中设置的参数(如使用的布尔值)被解析为select语句的参数。
这里的问题是,由于我已经开始使用缓存,因此不再解析布尔值。使用System.out.println调用该方法来评估使用的字段计算为null。这是有道理的,因为我在缓存管理器中创建了一个新的Profile对象,没有调用setter,除了cache get方法没有在getAllProfile方法中获取配置文件;它只需要大小。
我以为我可以通过在load方法中添加一个新的Profile参数来解决这个问题,如下所示:
@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
}
但是,load()似乎只是为了获取一个参数,所以这会出现这个错误:
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);
}
}
}
);
重申一下,我需要做的就是将Service层中创建的配置文件对象传递给管理器层和缓存。这似乎就像将第二个参数传递给load()一样简单,但这似乎不可能。
编辑:
我编辑了getAllProfiles方法以使用Callable:
Class 'Anonymous class derived from CacheLoader' must either be declared abstract or implement abstract method 'load(K)' in 'CacheLoader'
这会导致我传入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>
,所以我可以通过DAO中的字段解析SQL语句。
答案 0 :(得分:0)
这里是一个例子:
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);
}
}
因为不使用它,所以只给构建一个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);
});
});
}
在这里我调用cache get方法。 PS:useExecutors在后台线程中运行