本地缓存实现之Guava Cache
2017-11-26 17:13
459 查看
示例代码
import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalNotification; /** * 类说明:Guava缓存测试类 * * @author ruipeng.lrp * @since 2017/11/26 **/ public class GuavaCache { public static Cache<String, String> caches = CacheBuilder.newBuilder().maximumSize(128)// 设置容量上限 .expireAfterAccess(3, TimeUnit.SECONDS) // 若3s内没有读写请求则进行回收 .removalListener(new RemovalListener<String, String>() { // 移除监听器 @Override public void onRemoval(RemovalNotification<String, String> notification) { System.out.println(notification.getKey()); } }).build(); public static String get(String key) throws Exception { String rr = caches.get(key, new Callable<String>() { @Override public String call() throws Exception { if ("1".equalsIgnoreCase(key)) { return "test"; } throw new Exception("This is a test!"); } }); return rr; } public static void main(String[] args) throws Exception { System.out.println(GuavaCache.get("1")); System.out.println("before expire: " + GuavaCache.caches.asMap().keySet()); Thread.sleep(5000); System.out.println("after expire: " + GuavaCache.caches.asMap().keySet()); } }
相关文章推荐
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iOS学习笔记22—ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iPhone开发笔记(16)使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- <iOS>ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 基于Guava LoadingCache实现的本地缓存工具
- 在spring中利用Guava实现本地Cache的具体配置 第一篇<一>
- JAD-CACHE缓存框架,srping集成EhCache及实现本地缓存
- 本地缓存(Guava Loading Cache)
- Guava Cache 本地缓存组件浅析
- java java local cache本地缓存的两种实现,一个基于list轮询一个基于timer定时
- guava实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- JAD-CACHE缓存框架,srping集成EhCache及实现本地缓存
- Guava Cache 本地缓存组件浅析
- 第七章 企业项目开发--本地缓存guava cache