Google LoadingCache 无法存储加载的对象
Google LoadingCache Fails to Store Loaded Object
我正在使用 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 方法,它都保持为空。然而,CacheLoader 中的 load 方法确实会被调用,并且每次都会返回一个新的 Map 容器。似乎 LoadingCache 只是忽略了它加载的值。 contains
方法永远不会 returns 为真。
我想知道它是怎么发生的。
contains 方法永远不会 return 是因为点 class 没有正确实现 hashCode/equals 方法。或者您可以尝试使用 String 而不是 Point,那么包含将 return true.
原因并不明显。我在 class 加载时构造 laodingCache,值注入还没有完成。所以 expire
值始终为 0。简单修复:
@Value("${value}")
private int expire;
private LoadingCache<Integer, ConcurrentHashMap<Point, Boolean>> loadingCache;
@PostConstruct
public void init() {
loadingCache = CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterAccess(expire, TimeUnit.SECONDS)
.build(new CacheLoader<Integer, ConcurrentHashMap<Point, Boolean>>() {
@Override
public ConcurrentHashMap<Point, Boolean> load(Integer key) {
return new ConcurrentHashMap<>(2500);
}
});
}
值注入应该按预期方式工作。
我正在使用 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 方法,它都保持为空。然而,CacheLoader 中的 load 方法确实会被调用,并且每次都会返回一个新的 Map 容器。似乎 LoadingCache 只是忽略了它加载的值。 contains
方法永远不会 returns 为真。
我想知道它是怎么发生的。
contains 方法永远不会 return 是因为点 class 没有正确实现 hashCode/equals 方法。或者您可以尝试使用 String 而不是 Point,那么包含将 return true.
原因并不明显。我在 class 加载时构造 laodingCache,值注入还没有完成。所以 expire
值始终为 0。简单修复:
@Value("${value}")
private int expire;
private LoadingCache<Integer, ConcurrentHashMap<Point, Boolean>> loadingCache;
@PostConstruct
public void init() {
loadingCache = CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterAccess(expire, TimeUnit.SECONDS)
.build(new CacheLoader<Integer, ConcurrentHashMap<Point, Boolean>>() {
@Override
public ConcurrentHashMap<Point, Boolean> load(Integer key) {
return new ConcurrentHashMap<>(2500);
}
});
}
值注入应该按预期方式工作。