我之前在这里问过一个关于如何在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的Profile对象。这是为了在服务层上,用户可以使用@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调用该方法来计算employed字段的值为null。这是有意义的,因为我在缓存管理器中创建了一个新的配置文件对象,除了缓存get方法没有在getAllProfile方法中获取配置文件之外,没有调用任何setter方法;它只需要大小。
我想我可以通过在load方法中添加一个新的Profile参数来解决这个问题,如下所示:
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,以便可以解析SQL语句的DAO中的字段。
发布于 2018-11-09 23:47:28
下面是一个示例:
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一个空的返回值就行了,因为我们不使用它。
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在后台线程中运行它
https://stackoverflow.com/questions/50473913
复制相似问题