正在使用加载缓存来缓存查询结果。据我所知,如果在缓存中找不到 key ,它将从数据库加载。加载时会同时填充缓存吗?
CacheLoader <String, Obj> Loader = new CacheLoader<String, Obj>() {
@Override
public Obj load(String key) throws Exception {
Obj obj = ObjDao.getDs().createQuery(Obj.class).filter("id ==", key).get();
return obj;
}
};
最佳答案
Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.
Returns the value associated with
key
in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes.If another call to
get(K)
orgetUnchecked(K)
is currently loading the value forkey
, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.Caches loaded by a
CacheLoader
will callCacheLoader.load(K)
to load new values into the cache. Newly loaded values are added to the cache usingCache.asMap().putIfAbsent
after loading has completed; if another value was associated with key while the new value was loading then a removal notification will be sent for the new value.
关于java - 从数据库加载时填充加载缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171750/