博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Guava学习
阅读量:5012 次
发布时间:2019-06-12

本文共 3978 字,大约阅读时间需要 13 分钟。

转载:Guava

https://www.yiibai.com/guava/guava_caching_utilities.html

 

转载:

https://www.concretepage.com/google-api/google-guava-cache-example-using-loadingcache-cachebuilder-and-cacheloader

Google Guava Cache Example using LoadingCache, CacheBuilder and CacheLoader

By Arvind Rai, November 13, 2015

This page will walk through Google Guava cache example using LoadingCache, CacheBuilder and CacheLoader. Guava cache stores key and object like ConcurrentHashMap. Guava cache is thread safe. The feature provided by Guava cache is basically same as ConcurrentHashMap but Guava cache is more preferable than ConcurrentHashMap in terms of cache optimization. Find some features of Guava cache. 

1. We can define maximum capacity of cache to store key/object. 
2. We can set expiry time of cache. 
3. We can configure listener that listens any removal of cache element. 
4. We can configure concurrency level. 
Find the code snippet for using Guava cache.

LoadingCache
empCache = CacheBuilder.newBuilder() .maximumSize(100) .expireAfterWrite(10, TimeUnit.MINUTES) .build( new CacheLoader
() { @Override public Employee load(Integer id) throws Exception { return getEmployeeById(id); } } );

LoadingCache<K,V>

K: Base key type. 

V: Base value type. 
com.google.common.cache.LoadingCache loads data in cache automatically. Data remains in cache until it is evicted or cache is invalidated. LoadingCache is thread safe. This class provides different methods to access data for the given key. Some methods of this class are get(K key), asMap(), getUnchecked(K key), refresh(K key).

 

CacheBuilder<K,V>

com.google.common.cache.CacheBuilder is the builder for LoadingCache. Using CacheBuilder, we can configure cache settings. Find some methods of this class. 

concurrencyLevel(int concurrencyLevel) : It sets concurrency level among update operations. 
expireAfterAccess(long duration, TimeUnit unit) : We set the time to expire and once time is up, cache is expired after most resent access. 
expireAfterWrite(long duration, TimeUnit unit) : Cache is expired in a given duration after most resent write. 
maximumSize(long size) : Maximum size of entries that cache can store. 
removalListener(RemovalListener<? super K1,? super V1> listener) : Sets the listener that listens when any entry is removed.

CacheLoader<K,V>

com.google.common.cache.CacheLoader loads values for the given key and is cached with LoadingCache. We need to override load(K key) that returns the object to be cached. Within load(K key) method, we call our method which output needs to be cached. Normally we need to cache the output of the methods that performs some expensive calculation or database access which is being retrieved frequently.

Complete Example

build.gradle

apply plugin: 'java'apply plugin: 'eclipse' archivesBaseName = 'GoogleGuava' version = '1' repositories { mavenCentral() } dependencies { compile 'com.google.guava:guava:19.0-rc2' }

EmployeeGuavaCacheUtil.java

package com.concretepage; import java.util.concurrent.TimeUnit; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; public class EmployeeGuavaCacheUtil { private static LoadingCache
empCache; static { empCache = CacheBuilder.newBuilder() .maximumSize(100) .expireAfterWrite(10, TimeUnit.MINUTES) .build( new CacheLoader
() { @Override public Employee load(Integer id) throws Exception { return getEmployeeById(id); } } ); } public static LoadingCache
getLoadingCache() { return empCache; } public static Employee getEmployeeById(int id) { System.out.println("--Executing getEmployeeById--"); //Perform any expensive task like fetching data from Database. //For the demo we are performing a simple task Employee emp1 = new Employee(1, "Ramesh"); Employee emp2 = new Employee(2, "Mohan"); if(id == 1 ) { return emp1; } else

转载于:https://www.cnblogs.com/mybatis/p/9360899.html

你可能感兴趣的文章
面试题
查看>>
[LeetCode&Python] Problem 383. Ransom Note
查看>>
sh4.case语句
查看>>
在Fragment中加一个嵌套了ListView的ScrollView(一)
查看>>
《『若水新闻』客户端开发教程》——14.代码编写(6)
查看>>
【Linux】【Maven】Linux下安装和配置Maven
查看>>
uoj176 新年的繁荣
查看>>
[ZJOI2007]矩阵游戏
查看>>
单纯形法
查看>>
SQL中的replace函数
查看>>
java中的类型安全问题-Type safety: Unchecked cast from Object to ...
查看>>
翻译的一篇关于VBO的文章
查看>>
数据结构之表(C语言实现)
查看>>
php单例模式
查看>>
安卓开发第一天小结
查看>>
android GUI 流程记录
查看>>
C# 修饰符
查看>>
负载均衡介绍
查看>>
海贼王之——梦想音乐
查看>>
Windows Azure Cloud Service (18) 基于Input Endpoint通过Worker Role发布WCF服务
查看>>