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這是我開源的一個多級緩存框架的實現,若是有興趣能夠看一下