Spring Boot+Guava Cache+@EnableCaching
2018-01-09 15:03
916 查看
Spring Boot集成Guava Cache并配合@EnableCaching注解管理本地缓存
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.6-jre</version> </dependency>
Guava缓存集成
GuavaCacheConfig:@EnableConfigurationProperties(GuavaProperties.class) @EnableCaching @Configuration public class GuavaCacheConfig { @Autowired private GuavaProperties guavaProperties; @Bean public CacheBuilder<Object, Object> cacheBuilder() { long maximumSize = guavaProperties.getMaximumSize(); long duration = guavaProperties.getExpireAfterAccessDuration(); if (maximumSize <= 0) { maximumSize = 1024; } if (duration <= 0) { duration = 1800; } return CacheBuilder.newBuilder() .maximumSize(maximumSize) .expireAfterAccess(duration, TimeUnit.SECONDS); } /** * expireAfterAccess: 当缓存项在指定的时间段内没有被读或写就会被回收。 * expireAfterWrite:当缓存项在指定的时间段内没有更新就会被回收,如果我们认为缓存数据在一段时间后数据不再可用,那么可以使用该种策略。 * refreshAfterWrite:当缓存项上一次更新操作之后的多久会被刷新。 * @return */ @DependsOn({"cacheBuilder"}) @Bean public CacheManager cacheManager(CacheBuilder<Object, Object> cacheBuilder) { GuavaCacheManager cacheManager = new GuavaCacheManager(); cacheManager.setCacheBuilder(cacheBuilder); return cacheManager; } }
GuavaProperties:
@ConfigurationProperties(prefix = "guava.cache.config") public class GuavaProperties { private long maximumSize; private long maximumWeight; private long expireAfterWriteDuration; private long expireAfterAccessDuration; private long refreshDuration; private int initialCapacity; private int concurrencyLevel; // get/set忽略 }
为了方便测试设置缓存过期时间为10秒
application.properties:
server.port=8081 guava.cache.config.expire-after-access-duration=10
缓存测试
CacheService:@Service public class CacheService { @CachePut(value = "guavacache") public long save() { long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); System.out.println("进行缓存:" + timestamp); return timestamp; } @CacheEvict(value = "guavacache") public void delete() { System.out.println("删除缓存"); } @Cacheable(value = "guavacache") public long getByCache() { try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return new Timestamp(System.currentTimeMillis()).getTime(); } }
CacheController:
@RestController @RequestMapping("/cache") public class CacheController { @Autowired private CacheService cacheService; /** * 查询方法 */ @RequestMapping(value = "", method = RequestMethod.GET) public String getByCache() { Long startTime = System.currentTimeMillis(); long timestamp = this.cacheService.getByCache(); Long endTime = System.currentTimeMillis(); System.out.println("耗时: " + (endTime - startTime)); return timestamp+"ms"; } /** * 保存方法 */ @RequestMapping(value = "", method = RequestMethod.POST) public void save() { this.cacheService.save(); } /** * 删除方法 */ @RequestMapping(value = "", method = RequestMethod.DELETE) public void delete() { this.cacheService.delete(); } }
get方法访问localhost:8081/cache/,控制台输出:
耗时: 3007ms
在10s内访问,输出:
耗时: 1ms
说明,getByCache方法直接从缓存获取结果,线程没有执行到sleep方法,缓存成功。
其它方法可自行测试。
源码地址:
https://gitee.com/yidasanqian/spring-boot-guava-cache-demo.git
相关文章推荐
- spring boot guava cache 缓存学习
- Spring Boot cache backed guava/caffeine
- spring boot guava cache 缓存学习
- spring boot guava cache 缓存学习
- springboot2.0 redis EnableCaching的配置和使用
- 在 spring boot 中使用spring cache + redis
- spring boot之高级话题--@Enable*注解的工作原理(5)
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- springboot cache小记
- Spring Boot 数据缓存 Cache
- springboot 加入@EnableWebMvc不执行converter
- spring boot redis cache 缓存学习
- SpringBoot @EnableAutoConfiguration原理
- spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明
- 6 SpringBoot数据缓存Cache [Guava和Redis实现]
- spring boot spring cache ehcache3.x整合
- Spring Boot cache backed redis
- springboot 使用cache
- Springboot+cache 增删改查