SpringBoot怎么加入Guava Cache实现本地缓存
作者:水悠悠予安
时间:2023-05-10
浏览:0
在pom.xml中加入guava依赖com.google.guavaguava18.0创建一个CacheService,方便调用publicinterfaceCacheService{//存voidsetCommonCache(Stringkey,Objectvalue);//取ObjectgetCommonCache(Stringkey);}其实现类importcom.google.common.cache.Cache;importcom.google.common.cache.CacheBuilder
本文目录
在pom.xml中加入guava依赖
com.google.guava guava 18.0
创建一个CacheService,方便调用
public interface CacheService {
//存
void setCommonCache(String key,Object value);
//取
Object getCommonCache(String key);
}其实现类
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.wu.service.CacheService;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;
@Service
public class CacheServiceImpl implements CacheService {
private Cache commonCache=null;
@PostConstruct//代理此bean时会首先执行该初始化方法
public void init(){
commonCache= CacheBuilder.newBuilder()
//设置缓存容器的初始化容量为10(可以存10个键值对)
.initialCapacity(10)
//最大缓存容量是100,超过100后会安装LRU策略-最近最少使用,具体百度-移除缓存项
.maximumSize(100)
//设置写入缓存后1分钟后过期
.expireAfterWrite(60, TimeUnit.SECONDS).build();
}
@Override
public void setCommonCache(String key, Object value) {
commonCache.put(key,value);
}
@Override
public Object getCommonCache(String key) {
return commonCache.getIfPresent(key);
}
}
作者最新文章
拷贝漫画登录入口及官网地址
2026-11-24 09:35
漫蛙Manwa2入口整理 漫蛙漫画安全访问指南
2026-11-23 14:08
AO3作品历史怎么查?更新记录查看教程
2026-11-23 11:03
AO3最新官方入口分享及使用指南
2026-11-23 10:18
漫蛙2台版免费下载入口
2026-11-22 12:30
上一篇:
SpringBoot异常处理的原理分析
下一篇:
SpringBoot自动配置的原理分析
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
























