gpt4 book ai didi

java - Google LoadingCache 无法存储加载的对象

转载 作者:行者123 更新时间:2023-12-02 10:15:52 27 4
gpt4 key购买 nike

我使用 Map 容器作为 LoadingCache 中的值:

@Service
public class SlideCacheSpace {

@Value("${value}")
private int expire;

private LoadingCache<Integer, ConcurrentHashMap<Point, Boolean>> loadingCache = CacheBuilder.newBuilder()
.maximumSize(10000)
.expireAfterWrite(expire, TimeUnit.SECONDS)
.build(
new CacheLoader<Integer, ConcurrentHashMap<Point, Boolean>>() {
public ConcurrentHashMap<Point, Boolean> load(Integer key) {
return new ConcurrentHashMap<>(5000);
}
});


public boolean contains(int slug, Point point) {
ConcurrentHashMap<Point, Boolean> points = loadingCache.getIfPresent(slug);
if (points == null) {
return false;
}

return points.contains(point);
}

public void put(int slug, Point point) throws ExecutionException {
ConcurrentHashMap<Point, Boolean> points = loadingCache.get(slug);
points.put(point, true);
}

public void delete(int slug) {
loadingCache.invalidate(slug);
}
}

问题是我的 slideCacheSpace.put(key, val) 方法对 LoadingCache 没有任何影响。调用它后,LoadingCache 不会加载任何内容,并且仍然为空(在调试期间检查大小)。无论调用 put 方法多少次,它仍然为空。然而,每次返回新的 Map 容器时,都会调用 CacheLoader 中的 load 方法。 LoadingCache 似乎只是忽略了它加载的值。 contains 方法永远不会返回 true。

我想知道这是怎么发生的。

最佳答案

contains方法永远不会返回true,因为Point类没有正确实现hashCode/equals方法。或者你可以尝试使用 String 而不是 Point,那么 contains 将返回 true。

关于java - Google LoadingCache 无法存储加载的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688582/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com