SpringBoot缓存实战Caffeine示例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SpringBoot缓存实战Caffeine⽰例
Caffeine和Spring Boot集成
Caffeine是使⽤Java8对Guava缓存的重写版本,在Spring Boot 2.0中将取代Guava。
如果出现
Caffeine,CaffeineCacheManager将会⾃动配置。
使⽤spring.cache.cache-names属性可以在启动时创建缓存,并可以通过以下配置进⾏⾃定义(按顺序):
spring.cache.caffeine.spec:定义的特殊缓存
com.github.benmanes.caffeine.cache.CaffeineSpec: bean定义
com.github.benmanes.caffeine.cache.Caffeine: bean定义
例如,以下配置创建⼀个foo和bar缓存,最⼤数量为500,存活时间为10分钟:
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>,⾃动配置将忽略所有泛型类型。
引⼊依赖
<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开启对缓存的⽀持
@SpringBootApplication
@EnableCaching// 开启缓存,需要显⽰的指定
public class SpringBootStudentCacheCaffeineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStudentCacheCaffeineApplication.class, args);
}
}
配置⽂件
新增对缓存的特殊配置,如最⼤容量、过期时间等
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;
}
Caffeine配置说明:
1. initialCapacity=[integer]: 初始的缓存空间⼤⼩
2. maximumSize=[long]: 缓存的最⼤条数
3. maximumWeight=[long]: 缓存的最⼤权重
4. expireAfterAccess=[duration]: 最后⼀次写⼊或访问后经过固定时间过期
5. expireAfterWrite=[duration]: 最后⼀次写⼊后经过固定时间过期
6. refreshAfterWrite=[duration]: 创建缓存或者最近⼀次更新缓存后经过固定的时间间隔,刷新缓存
7. weakKeys: 打开key的弱引⽤
8. weakValues:打开value的弱引⽤
9. softValues:打开value的软引⽤
10. recordStats:开发统计功能
注意:
1. expireAfterWrite和expireAfterAccess同事存在时,以expireAfterWrite为准。
2. maximumSize和maximumWeight不可以同时使⽤
3. weakValues和softValues不可以同时使⽤
⽰例代码
/**
* @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);
("为id、key为:" + p.getId() + "数据做了缓存");
return p;
}
@Override
@CacheEvict(value = "people")//2
public void remove(Long id) {
("删除了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());
("为id、key为:" + p.getId() + "数据做了缓存");
return p;
}
@Override
@Cacheable(value = "people1")//3
public Person findOne1() {
Person p = personRepository.findOne(2L);
("为id、key为:" + p.getId() + "数据做了缓存");
return p;
}
@Override
@Cacheable(value = "people2")//3
public Person findOne2(Person person) {
Person p = personRepository.findOne(person.getId());
("为id、key为:" + p.getId() + "数据做了缓存");
return p;
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。