Springboot緩存
啟動(dòng)緩存@EnableCaching
@Cacheable
@Cacheable
注解就可以將運(yùn)行結(jié)果緩存,以后查詢相同的數(shù)據(jù),直接從緩存中取,不需要調(diào)用方法。
@Cacheable(cacheNames = "findAll")
@Override
public List<BookType> findAll() {
List<BookType> bookTypes = bookTypeDao.selectList(null);
return bookTypes;
}
序列 | 參數(shù) | 解釋 |
---|---|---|
1 | cacheNames | 指定緩存組件的名字 |
2 | key | 緩存數(shù)據(jù)時(shí)使用的key,默認(rèn)使用方法參數(shù) |
3 | keyGenerator | key 的生成器。 key 和 keyGenerator 二選一使用 |
4 | cacheManager | 可以用來指定緩存管理器。從哪個(gè)緩存管理器里面獲取緩存。 |
5 | condition | 可以用來指定符合條件的情況下才緩存 |
6 | unless | 否定緩存。當(dāng) unless 指定的條件為 true ,方法的返回值就不會(huì)被緩存。當(dāng)然你也可以獲取到結(jié)果進(jìn)行判斷 |
7 | sync | 是否使用異步模式。 |
@Cacheable(cacheNames = "findByName",key = "#name")
@Override
public List<BookType> findByName(String name) {
LambdaQueryWrapper<BookType> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(BookType::getName, name);
List<BookType> bookTypes = bookTypeDao.selectList(queryWrapper);
return bookTypes;
}
@CachePut
@CachePut也可以聲明一個(gè)方法支持緩存功能。與@Cacheable不同的是使用@CachePut標(biāo)注的方法在執(zhí)行前不會(huì)去檢查緩存中是否存在之前執(zhí)行過的結(jié)果,而是每次都會(huì)執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對(duì)的形式存入指定的緩存中。
@CachePut(cacheNames = "findAll")
@Override
public List<BookType> findAll() {
List<BookType> bookTypes = bookTypeDao.selectList(null);
return bookTypes;
}
@CacheEvict
@CacheEvict是用來標(biāo)注在需要清除緩存元素的方法或類上的。當(dāng)標(biāo)記在一個(gè)類上時(shí)表示其中所有的方法的執(zhí)行都會(huì)觸發(fā)緩存的清除操作。
key: 從緩存塊中移出的key值文章來源:http://www.zghlxwxcb.cn/news/detail-836259.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-836259.html
@Cacheable(cacheNames = "privses",key = "#id")
@Override
public Privs findById(Long id) {
return privsDao.findById(id).get();
}
@CacheEvict(cacheNames = "privses",key="#id")
@Override
public void delById(long id) {
// privsDao.deleteById(id);
System.out.println("數(shù)據(jù)庫刪除:"+id+"完成....,更新緩存");
}
到了這里,關(guān)于springboot2入門到實(shí)戰(zhàn)-spring緩存的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!