1.ehcache缓存
需要添加依赖的jar
implementation 'net.sf.ehcache:ehcache:2.10.6'
implementation 'org.springframework.boot:spring-boot-starter-cache:2.1.2.RELEASE'
application.yml
#缓冲的配置
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
然后,在springboot启动类添加启动缓冲注解 @EnableCaching
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!-- java.io.tmpdir是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样 Windows: java.io.tmpdir:[C:Users登录用户~1AppDataLocalTemp] Solaris: java.io.tmpdir:[/var/tmp/] Linux/MACOS: java.io.tmpdir: [/tmp] --> <diskStore path="java.io.tmpdir"/> <!-- 默认缓存 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <!-- 定义缓存 时间设置2分钟--> <cache name="table1Cache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" 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>
import com.syiass.domain.TagInfo;
import com.syiass.repository.TagInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import java.util.List;
//标注数据访问组件,即DAO组件
/*
参数 value缓存的名称必须指定至少一个
缓存的key可以为空
eg: key 定义的id是传入的数值
@Cacheable(value=”testcache”,key=”#id”)
*/
// cacheNames为ehcache.xml配置文件内的配置名称
@CacheConfig(cacheNames = {"table1Cache"})
@Repository
public class TaginfoEhcache {
@Autowired
private TagInfoRepository tagInfoRepository;
/*
@Cacheable:应用到读取数据的方法上,即可缓存的方法,
如查找方法:先从缓存中读取,如果没有再调用方法获取数据,
然后把数据添加到缓存中,适用于查找;
value 指的是 ehcache.xml 中的缓存策略 既name的数值
*/
@Cacheable(value= "table1Cache")
public List<TagInfo> getAllList(){
return
tagInfoRepository.getAllList();
}
}
2.guava缓冲
分别是maven和gradle依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
<type>bundle</type>
</dependency>
implementation 'com.google.guava:guava:20.0'
封装工具类
package com.syiaas.common;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
public class TokenCache {
private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
public static final String TOKEN_PREFIX = "token_";
// guava本地缓冲 设置缓冲最大容量,若超过会采用LRU算法就是最少使用移除缓冲项 过期时间12个小时
private static LoadingCache<String,String> localCache = CacheBuilder.newBuilder().initialCapacity(1000)
.maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
.build(new CacheLoader<String, String>() {
//默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载
@Override
public String load(String s) throws Exception {
return "null";
}
});
public static void setKey(String key,String value){
localCache.put(key,value);
}
public static String getKey(String key){
String value = null;
try {
value = localCache.get(key);
if ("null".equals(value)){
return null;
}
return value;
}catch (Exception e){
logger.error("localCache get error",e);
}
return null;
}
}
转载于:https://my.oschina.net/shanesen/blog/3017690
最后
以上就是酷酷白猫最近收集整理的关于Springboot整合ehcache及guava作为缓冲使用的方式的全部内容,更多相关Springboot整合ehcache及guava作为缓冲使用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复