I'm using a Map container as the value in 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);
}
}
The issue is my slideCacheSpace.put(key, val)
method doesn't take any effect on the LoadingCache. After calling it, the LoadingCache doesn't load anything and is still empty (inspected size during debug). It remains empty no matter how many times the put method is called. The load method in the CacheLoader, however, does get called and every time a new Map container is returned. It seems that the LoadingCache just ignores the value it loads. The contains
method never returns true.
I'm wondering how it could happen.
loadingCache
. For map view useloadingCache.asMap()
. – Xaerxess