java - 从数据库加载时填充加载缓存

标签 java caching guava

正在使用加载缓存来缓存查询结果。据我所知,如果在缓存中找不到 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;                                   
        }
};

最佳答案

LoadingCache :

Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

LoadingCache.get(K) :

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.

关于java - 从数据库加载时填充加载缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171750/

相关文章:

php - iOS 显然永久缓存网页

PHP:缓存 RSS 数据以避免每次加载页面时都请求它

java - 无法在使用 Guava 的 Eclipse 中获得内容帮助

java - 检测使用的是哪个 Guava 版本?

java - 安卓开发: “thread exiting with uncaught exception”

java - OpenShift Pro - 实时和测试站点 - 最佳实践/替代方案

java - 自动导入包的顺序和歧义

java - 输入到csv文件中的包含逗号的字符串

http - 如何指示网络浏览器不要缓存页面?

java - IllegalStateException : Can't overwrite cause in Guava map. 放