Spring Boot緩存實戰(zhàn) Caffeine示例
Caffeine和Spring Boot集成
Caffeine是使用Java8對Guava緩存的重寫版本,在Spring Boot 2.0中將取代Guava。如果出現(xiàn)Caffeine,CaffeineCacheManager將會自動配置。使用spring.cache.cache-names屬性可以在啟動時創(chuàng)建緩存,并可以通過以下配置進行自定義(按順序):
- spring.cache.caffeine.spec: 定義的特殊緩存
- com.github.benmanes.caffeine.cache.CaffeineSpec: bean定義
- com.github.benmanes.caffeine.cache.Caffeine: bean定義
例如,以下配置創(chuàng)建一個foo和bar緩存,最大數(shù)量為500,存活時間為10分鐘:
spring.cache.cache-names=foo,bar spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
除此之外,如果定義了com.github.benmanes.caffeine.cache.CacheLoader,它會自動關(guān)聯(lián)到CaffeineCacheManager。由于該CacheLoader將關(guān)聯(lián)被該緩存管理器管理的所有緩存,所以它必須定義為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配置說明:
- initialCapacity=[integer]: 初始的緩存空間大小
- maximumSize=[long]: 緩存的最大條數(shù)
- maximumWeight=[long]: 緩存的最大權(quán)重
- expireAfterAccess=[duration]: 最后一次寫入或訪問后經(jīng)過固定時間過期
- expireAfterWrite=[duration]: 最后一次寫入后經(jīng)過固定時間過期
- refreshAfterWrite=[duration]: 創(chuàng)建緩存或者最近一次更新緩存后經(jīng)過固定的時間間隔,刷新緩存
- weakKeys: 打開key的弱引用
- weakValues:打開value的弱引用
- softValues:打開value的軟引用
- recordStats:開發(fā)統(tǒng)計功能
注意:
- expireAfterWrite和expireAfterAccess同事存在時,以expireAfterWrite為準(zhǔn)。
- maximumSize和maximumWeight不可以同時使用
- 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);
logger.info("為id、key為:" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
@Override
@CacheEvict(value = "people")//2
public void remove(Long id) {
logger.info("刪除了id、key為" + id + "的數(shù)據(jù)緩存");
//這里不做實際刪除操作
}
/**
* Cacheable
* value:緩存key的前綴。
* key:緩存key的后綴。
* sync:設(shè)置如果緩存過期是不是只放一個請求去請求數(shù)據(jù)庫,其他請求阻塞,默認(rèn)是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() + "數(shù)據(jù)做了緩存");
return p;
}
@Override
@Cacheable(value = "people1")//3
public Person findOne1() {
Person p = personRepository.findOne(2L);
logger.info("為id、key為:" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
@Override
@Cacheable(value = "people2")//3
public Person findOne2(Person person) {
Person p = personRepository.findOne(person.getId());
logger.info("為id、key為:" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
}
源碼:https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot+spring cache實現(xiàn)兩級緩存(redis+caffeine)
- Springboot Caffeine本地緩存使用示例
- Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)
- SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine)
- SpringBoot集成Caffeine緩存的實現(xiàn)步驟
- SpringBoot 緩存 Caffeine使用解析
- Spring?Cache?集成?Caffeine實現(xiàn)項目緩存的示例
- springboot集成本地緩存Caffeine的三種使用方式(小結(jié))
- 基于Spring接口集成Caffeine+Redis兩級緩存
相關(guān)文章
Java導(dǎo)出Word文檔的實現(xiàn)方法詳解
這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Word文檔的實現(xiàn)方法,在日常的開發(fā)工作中,我們時常會遇到導(dǎo)出Word文檔報表的需求,比如公司的財務(wù)報表、醫(yī)院的患者統(tǒng)計報表、電商平臺的銷售報表等等,需要的朋友可以參考下2023-08-08
springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

