Springboot整合GuavaCache缓存过程解析
这篇文章主要介绍了springboot整合GuavaCache缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
GuavaCache是一种本地缓存机制,之所以叫本地缓存,是因为它不会把缓存数据放到外部文件或者其他服务器上,而是存放到了应用内存中。
GuavaCache的优点是:简单、强大、轻量级。
GuavaCache适用场景:
1.某些接口或者键值会被查询多次以上;
2.愿意使用或牺牲一些内存空间来提升访问或者计算速度;
3.缓存内容或者结果值较小,不会超过内存总容量;
GuavaCache中基于注解的声明式缓存操作
@Cacheable触发缓存逻辑
Spring在执行@Cacheable标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
参数:value缓存名、key缓存键值、condition满足缓存条件、unless否决缓存条件
@CacheEvict
触发缓存逐出逻辑
方法执行成功后会从缓存中移除相应数据。
参数:value缓存名、key缓存键值、condition满足缓存条件、unless否决缓存条件、allEntries是否移除所有数据(设置为true时会移除所有缓存)
@CachePut
和@Cacheable类似,但会把方法的返回值放入缓存中,主要用于数据新增和修改方法。
pom.xml配置文件:
org.springframework.boot spring-boot-starter-cache 1.5.9.RELEASE com.google.guava guava 19.0
GuavaCacheConfig:
/** *Copyright(c)2020,AllRightsReserved. * */ packagecom.demo.server.config; importjava.util.concurrent.TimeUnit; importorg.springframework.cache.CacheManager; importorg.springframework.cache.annotation.EnableCaching; importorg.springframework.cache.guava.GuavaCacheManager; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importcom.google.common.cache.CacheBuilder; @Configuration @EnableCaching publicclassGuavaCacheConfig{ @Bean publicCacheManagercacheManager(){ GuavaCacheManagercacheManager=newGuavaCacheManager(); cacheManager.setCacheBuilder( CacheBuilder.newBuilder(). expireAfterWrite(10,TimeUnit.SECONDS).//存活时间10秒 maximumSize(1000));//最大线程数 returncacheManager; } }
CacheController:
/** *Copyright(c)2020,AllRightsReserved. * */ packagecom.demo.server.guava; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/guava") publicclassCacheController{ @Autowired privateCacheServicecacheService; /** *查询方法 */ @RequestMapping(value="/get",method=RequestMethod.GET) publicStringgetByCache(){ LongstartTime=System.currentTimeMillis(); longtimestamp=this.cacheService.getByCache(); LongendTime=System.currentTimeMillis(); System.out.println("耗时:"+(endTime-startTime)); returntimestamp+""; } /** *保存方法 */ @RequestMapping(value="/save",method=RequestMethod.POST) publicvoidsave(){ this.cacheService.save(); } /** *删除方法 */ @RequestMapping(value="delete",method=RequestMethod.DELETE) publicvoiddelete(){ this.cacheService.delete(); } }
CacheService:
/** *Copyright(c)2020,AllRightsReserved. * */ packagecom.demo.server.guava; importjava.sql.Timestamp; importorg.springframework.cache.annotation.CacheEvict; importorg.springframework.cache.annotation.CachePut; importorg.springframework.cache.annotation.Cacheable; importorg.springframework.stereotype.Service; @Service publicclassCacheService{ @CachePut(value="guavacache") publiclongsave(){ longtimestamp=newTimestamp(System.currentTimeMillis()).getTime(); System.out.println("进行缓存:"+timestamp); returntimestamp; } @CacheEvict(value="guavacache") publicvoiddelete(){ System.out.println("删除缓存"); } @Cacheable(value="guavacache") publiclonggetByCache(){ try{ Thread.sleep(3*1000); }catch(InterruptedExceptione){ e.printStackTrace(); } returnnewTimestamp(System.currentTimeMillis()).getTime(); } }
Application:
packagecom.demo; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.boot.Banner; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication publicclassApplication{ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(Application.class); publicstaticvoidmain(String[]args){ SpringApplicationapp=newSpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.setWebEnvironment(true); app.run(args); LOG.info("****************StartupSuccess****************"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。