SpringBoot中怎么利用GuavaCache实现本地缓存-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
SpringBoot中怎么利用GuavaCache实现本地缓存-创新互联

SpringBoot中怎么利用GuavaCache实现本地缓存,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联公司主营临泽网站建设的网络公司,主营网站建设方案,app软件开发,临泽h5微信平台小程序开发搭建,临泽网站营销推广欢迎临泽等地区企业咨询

在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;@Servicepublic 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中怎么利用GuavaCache实现本地缓存-创新互联
标题网址:http://scgulin.cn/article/pppgp.html