Caffeine是使用Java8对Guava缓存的重写版本,在Spring Boot 2.0中将取代Guava。若是出现Caffeine,CaffeineCacheManager将会自动配置。使用spring.cache.cache-names属性能够在启动时建立缓存,并能够经过如下配置进行自定义(按顺序):html
例如,如下配置建立一个foo和bar缓存,最大数量为500,存活时间为10分钟:java
spring.cache.cache-names=foo,bar spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
除此以外,若是定义了com.github.benmanes.caffeine.cache.CacheLoader,它会自动关联到CaffeineCacheManager。因为该CacheLoader将关联被该缓存管理器管理的全部缓存,因此它必须定义为CacheLoader<Object, Object>,自动配置将忽略全部泛型类型。git
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.6.0</version> </dependency>
使用@EnableCaching注解让Spring Boot开启对缓存的支持github
@SpringBootApplication @EnableCaching// 开启缓存,须要显示的指定 public class SpringBootStudentCacheCaffeineApplication { public static void main(String[] args) { SpringApplication.run(SpringBootStudentCacheCaffeineApplication.class, args); } }
新增对缓存的特殊配置,如最大容量、过时时间等spring
spring.cache.cache-names=people spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=10s,refreshAfterWrite=5s
若是使用了refreshAfterWrite配置还必须指定一个CacheLoader,如:数据库
/** * 必需要指定这个Bean,refreshAfterWrite=5s这个配置属性才生效 * * @return */ @Bean public CacheLoader<Object, Object> cacheLoader() { CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() { @Override public Object load(Object key) throws Exception { return null; } // 重写这个方法将oldValue值返回回去,进而刷新缓存 @Override public Object reload(Object key, Object oldValue) throws Exception { return oldValue; } }; return cacheLoader; }
/** * @author yuhao.wang */ @Service public class PersonServiceImpl implements PersonService { private static final Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class); @Autowired PersonRepository personRepository; @Override @CachePut(value = "people", key = "#person.id") public Person save(Person person) { Person p = personRepository.save(person); logger.info("为id、key为:" + p.getId() + "数据作了缓存"); return p; } @Override @CacheEvict(value = "people")//2 public void remove(Long id) { logger.info("删除了id、key为" + id + "的数据缓存"); //这里不作实际删除操做 } /** * Cacheable * value:缓存key的前缀。 * key:缓存key的后缀。 * sync:设置若是缓存过时是否是只放一个请求去请求数据库,其余请求阻塞,默认是false。 */ @Override @Cacheable(value = "people", key = "#person.id", sync = true) public Person findOne(Person person, String a, String[] b, List<Long> c) { Person p = personRepository.findOne(person.getId()); logger.info("为id、key为:" + p.getId() + "数据作了缓存"); return p; } @Override @Cacheable(value = "people1")//3 public Person findOne1() { Person p = personRepository.findOne(2L); logger.info("为id、key为:" + p.getId() + "数据作了缓存"); return p; } @Override @Cacheable(value = "people2")//3 public Person findOne2(Person person) { Person p = personRepository.findOne(person.getId()); logger.info("为id、key为:" + p.getId() + "数据作了缓存"); return p; } }
参考:缓存
https://memorynotfound.com/spring-boot-caffeine-caching-example-configuration/springboot
http://xp-developer.com/html/springboot/IV.%20Spring%20Boot%20features/31.1.8%20Caffeine
源码: https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
spring-boot-student-cache-caffeine 工程
为监控而生的多级缓存框架 layering-cache这是我开源的一个多级缓存框架的实现,若是有兴趣能够看一下