从数据库加载时填充加载缓存
Populating loading cache while loading from DB
正在使用加载缓存来缓存查询结果。据我所知,如果在缓存中找不到密钥,它将从数据库加载。加载时是否会同时填充缓存?
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)
or getUnchecked(K)
is currently loading the value for key
, 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 call CacheLoader.load(K)
to load new values into the cache. Newly loaded values are added to the cache using Cache.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.
正在使用加载缓存来缓存查询结果。据我所知,如果在缓存中找不到密钥,它将从数据库加载。加载时是否会同时填充缓存?
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.