Caffeine and Spring Boot Integration
Caffeine is a rewritten version of Guava cache using Java 8, which will replace Guava in Spring Boot 2.0. If Caffeine appears, CaffeineCacheManager will be automatically configured. Use the spring.cache.cache-names property to create caches at startup and can be customized (in order) with the following configuration:
For example, the following configuration creates a foo and bar cache with a maximum number of 500 and a survival time of 10 minutes:
spring.cache.cache-names=foo,barspring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
In addition, if com.github.benmanes.caffeine.cache.CacheLoader is defined, it will automatically be associated with the CaffeineCacheManager. Since the CacheLoader will associate all caches managed by the cache manager, it must be defined as CacheLoader<Object, Object>, and automatic configuration will ignore all generic types.
Introduce dependencies
<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>
Enable cache support
Use the @EnableCaching annotation to enable cache support
@SpringBootApplication@EnableCaching// Enable cache, the specified public class SpringBootStudentCacheCaffeineApplication { public static void main(String[] args) { SpringApplication.run(SpringBootStudentCacheCaffeineApplication.class, args); }}Configuration File
Added special configurations for cache, such as maximum capacity, expiration time, etc.
spring.cache.cache-names=peoplespring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=10s,refreshAfterWrite=5s
If refreshAfterWrite configuration is used, a CacheLoader must also be specified, such as:
/** * This Bean must be specified, and the configuration property of refreshAfterWrite=5s will take effect* * @return */@Beanpublic CacheLoader<Object, Object> cacheLoader() { CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() { @Override public Object load(Object key) throws Exception { return null; } // Rewrite this method and return the oldValue value back, and then refresh the cache @Override public Object reload(Object key, Object oldValue) throws Exception { return oldValue; } }; return cacheLoader;}Caffeine configuration instructions:
Notice:
Sample code
/** * @author yuhao.wang */@Servicepublic 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(" is id, key:" + p.getId() + "data cached"); return p; } @Override @CacheEvict(value = "people")//2 public void remove(Long id) { logger.info("Remove data cache with id, key " + id + ""); //The actual delete operation is not performed here} /** * Cacheable * value: cache the prefix of the key. * key: cache key suffix. * sync: Set if the cache expires, is it necessary to only place one request to request the database, and other requests are blocked, and the default is 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("For id, key:" + p.getId() + "Data is cached"); return p; } @Override @Cacheable(value = "people1")//3 public Person findOne1() { Person p = personRepository.findOne(2L); logger.info("For id, key is:" + p.getId() + "Data is cached"); return p; } @Override @Cacheable(value = "people2")//3 public Person findOne2(Person person) { Person p = personRepository.findOne(person.getId()); logger.info("For id, key is:" + p.getId() + "Data is cached"); return p; }} Source code: https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.