Guava Cache
Google Guava Cache是一种非常优秀本地缓存解决方案,提供了基于容量,时间和引用的缓存回收方式。
怎么用?
引入依赖
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>18.0</version></dependency>
相关加载缓存代码
publicclassGiteaUserServiceImplimplementsGiteaUserService { // 缓存的listprivateLoadingCache<String, Optional<CommonUserDto>>emailUserList; // 在项目启动的时候构造缓存publicvoidinit() { // 设置两小时的过期时间emailUserList=CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.HOURS).build(newCacheLoader<String, Optional<CommonUserDto>>() { // 缓存失效之后的重新加载逻辑publicOptional<CommonUserDto>load(Stringemail) { CommonUserDtocommonUserDto=null; try { commonUserDto=giteaExService.qryUserDtoByEmail(email); } catch (Exceptione) { logger.error("LoadingCache Portal User error...", e); } returnOptional.ofNullable(commonUserDto); } }); // 加载缓存this.loadUserDtoList(); } /*** 查询并且构造用户集合*/privatevoidloadUserDtoList() { // 查询全量用户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><diskStorepath = "./cache"/><!-- 默认的管理策略 --><defaultCacheeternal = "false"maxElementsInMemory = "10000"overflowToDisk = "true"diskPersistent = "false"timeToIdleSeconds = "604800"timeToLiveSeconds = "7200"diskExpiryThreadIntervalSeconds = "120"memoryStoreEvictionPolicy = "LRU"/><!-- 此缓存最多可以存活timeToLiveSeconds秒,如果期间超过timeToIdleSeconds秒未访问,缓存失效 --><cachename = "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
publicclassEhCacheConfig { /*** 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.*/publicEhCacheManagerFactoryBeanehCacheManagerFactoryBean() { EhCacheManagerFactoryBeancacheManagerFactoryBean=newEhCacheManagerFactoryBean(); cacheManagerFactoryBean.setConfigLocation(newClassPathResource("ehcache.xml")); cacheManagerFactoryBean.setShared(true); returncacheManagerFactoryBean; } }
写一个缓存工具类
publicclassCacheUtils { privatestaticCachecache; privatestaticfinalStringCACHE_NAME="defaultCache"; privateEhCacheCacheManagerehCacheCacheManager; publicstaticvoidput(Stringkey, Objectvalue) { put(CACHE_NAME, key, value); } publicstaticObjectget(Stringkey) { Elementelement=cache.get(key); if (null!=element) { returnelement.getObjectValue(); } returnelement; } privatevoidinit() { EhCacheCacheManagerehCacheCacheManager=ApplicationContextHelper.getBean(EhCacheCacheManager.class); CacheManagercacheManager=ehCacheCacheManager.getCacheManager(); try { cache=cacheManager.getCache(CACHE_NAME); if (cache==null) { thrownewRuntimeException("当前系统中没有定义“"+CACHE_NAME+"”这个缓存。"); } } catch (Exceptionex) { ex.printStackTrace(); } } /*** 写入缓存 通用方法** @param cacheName* @param key* @param value*/privatestaticvoidput(StringcacheName, Stringkey, Objectvalue) { Elementelement=newElement(key, value); cache.put(element); } }
以上是两种本地缓存的框架的简单使用,个人使用来看Google的缓存更加简单实用。