Guava Cache
Google Guava Cache是一种非常优秀本地缓存解决方案,提供了基于容量,时间和引用的缓存回收方式。
怎么用?
引入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
相关加载缓存代码
public class GiteaUserServiceImpl implements GiteaUserService {
// 缓存的list
private LoadingCache<String, Optional<CommonUserDto>> emailUserList;
// 在项目启动的时候构造缓存
@PostConstruct
public void init() {
// 设置两小时的过期时间
emailUserList = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.HOURS).build(new CacheLoader<String, Optional<CommonUserDto>>() {
// 缓存失效之后的重新加载逻辑
@Override
public Optional<CommonUserDto> load(String email) {
CommonUserDto commonUserDto = null;
try {
commonUserDto = giteaExService.qryUserDtoByEmail(email);
}
catch (Exception e) {
logger.error("LoadingCache Portal User error...", e);
}
return Optional.ofNullable(commonUserDto);
}
});
// 加载缓存
this.loadUserDtoList();
}
/**
* 查询并且构造用户集合
*/
private void loadUserDtoList() {
// 查询全量用户
List<CommonUserDto> userDtoList = giteaExService.qryUserDtoList();
// 加载缓存
userDtoList.stream()
.filter(commonUserDto -> !ObjectUtils.isEmpty(commonUserDto) && StringUtils.isNotEmpty(commonUserDto.getEmail()))
.forEach(commonUserDto -> {
// 构造email缓存
emailUserList.put(commonUserDto.getEmail(), Optional.of(commonUserDto));
});
}
}
load方法是缓存失效之后的重新加载逻辑,因为guava cache不允许出现null,所以使用Optional作为容器,设置null;
ehcache
Ehcache主要基于内存缓存,磁盘缓存为辅的,使用起来方便。支持缓存内容存储在磁盘中。
使用方式
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
在项目资源目录下新建一个ehcache.xml文件,文件内容参考如下:
<ehcache>
<diskStore path = "./cache"/>
<!-- 默认的管理策略 -->
<defaultCache
eternal = "false"
maxElementsInMemory = "10000"
overflowToDisk = "true"
diskPersistent = "false"
timeToIdleSeconds = "604800"
timeToLiveSeconds = "7200"
diskExpiryThreadIntervalSeconds = "120"
memoryStoreEvictionPolicy = "LRU"/>
<!-- 此缓存最多可以存活timeToLiveSeconds秒,如果期间超过timeToIdleSeconds秒未访问,缓存失效 -->
<cache
name = "defaultCache"
eternal = "false"
maxElementsInMemory = "80000"
overflowToDisk = "true"
diskPersistent = "false"
timeToIdleSeconds = "604800"
timeToLiveSeconds = "604800"
memoryStoreEvictionPolicy = "LRU"/>
<!-- maxElementsInMemory 内存中最大缓存对象数,看着自己的heap大小来搞 -->
<!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false -->
<!-- maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大 -->
<!-- overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,
会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。-->
<!-- diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。-->
<!-- diskPersistent:是否缓存虚拟机重启期数据 -->
<!-- diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒 -->
<!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,
如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,
EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,
则表示对象可以无限期地处于空闲状态 -->
<!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,
如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,
EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,
则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义 -->
<!-- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,
Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、
FIFO(先进先出)、LFU(最少访问次数)。-->
</ehcache>
注入bean的方式构造cacheManager
@Configuration
public class EhCacheConfig {
/**
* 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
*/
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
}
写一个缓存工具类
@Component
@EnableCaching
public class CacheUtils {
private static Cache cache;
private static final String CACHE_NAME = "defaultCache";
@Autowired
private EhCacheCacheManager ehCacheCacheManager;
public static void put(String key, Object value) {
put(CACHE_NAME, key, value);
}
public static Object get(String key) {
Element element = cache.get(key);
if (null != element) {
return element.getObjectValue();
}
return element;
}
@PostConstruct
private void init() {
EhCacheCacheManager ehCacheCacheManager = ApplicationContextHelper.getBean(EhCacheCacheManager.class);
CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
try {
cache = cacheManager.getCache(CACHE_NAME);
if (cache == null) {
throw new RuntimeException("当前系统中没有定义“" + CACHE_NAME + "”这个缓存。");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 写入缓存 通用方法
*
* @param cacheName
* @param key
* @param value
*/
private static void put(String cacheName, String key, Object value) {
Element element = new Element(key, value);
cache.put(element);
}
}
以上是两种本地缓存的框架的简单使用,个人使用来看Google的缓存更加简单实用。