SpringBoot加入GuavaCache实现本地缓存的示例分析
小编给大家分享一下SpringBoot加入Guava Cache实现本地缓存的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站于2013年创立,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元博州做网站,已为上家服务,为博州各地企业和个人服务,联系电话:18980820575
在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);
}
} 以上是“SpringBoot加入Guava Cache实现本地缓存的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
本文题目:SpringBoot加入GuavaCache实现本地缓存的示例分析
分享地址:http://www.jxjierui.cn/article/poeodc.html


咨询
建站咨询
