簡體   English   中英

如何在 java 中緩存 couchbase 文檔?

[英]how can i cache couchbase documents in java?

我必須緩存執行以下查詢時返回的所有文檔

select * from `contact` where type="org"

這樣做的最佳方法是什么?

使用 spring 緩存並在您的應用程序配置 class 上添加 @EnableCaching,並聲明一個將返回 CacheManager bean 的方法,

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;

private static final String cacheName ="YourCacheMap";
private static final String cacheManager ="AppCacheManager";

@Bean(name = "AppCacheManager")
public CacheManager cacheManager(){
    final SimpleCacheManager cacheManager = new SimpleCacheManager();
    final List<AbstractValueAdaptingCache> caches = new ArrayList<>();
    final ConcurrentMapCache cacheMap = new ConcurrentMapCache("YourCacheMap",
            CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
    caches.add(cacheMap);
    cacheManager.setCaches(caches);
    return cacheManager;
}

並且在您的存儲庫服務 class 中,在您要緩存的方法上使用 @Cacheable 注釋

@Cacheable(value = cacheName, cacheManager = cacheManager, key = "#p0"
        ,unless = "#result == null || #p0==null")
public List<YourDataType> getResult(String type) {
    // here you will return your results
    return new ArrayList<YourDataType>();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM