日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

發(fā)布時間:2025/3/17 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文源碼 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base

一、Cache緩存簡介

從Spring3開始定義Cache和CacheManager接口來統(tǒng)一不同的緩存技術(shù);
Cache接口為緩存的組件規(guī)范定義,包含緩存的各種操作集合;
Cache接口下Spring提供了各種緩存的實現(xiàn);
如RedisCache,EhCacheCache ,ConcurrentMapCache等;

二、核心API

1、Cache緩存接口
定義緩存操作。實現(xiàn)有:RedisCache、EhCacheCache、ConcurrentMapCache等
2、CacheManager
緩存管理器,管理各種緩存(cache)組件
3、@Cacheable 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其進行緩存

Cacheable 執(zhí)行流程 1)方法運行之前,按照cacheNames指定的名字先去查詢Cache 緩存組件 2)第一次獲取緩存如果沒有Cache組件會自動創(chuàng)建 3)Cache中查找緩存的內(nèi)容,使用一個key,默認(rèn)就是方法的參數(shù) 4)key是按照某種策略生成的;默認(rèn)是使用keyGenerator生成的,這里使用自定義配置 5)沒有查到緩存就調(diào)用目標(biāo)方法; 6)將目標(biāo)方法返回的結(jié)果,放進緩存中Cacheable 注解屬性 cacheNames/value:指定方法返回結(jié)果使用的緩存組件的名字,可以指定多個緩存 key:緩存數(shù)據(jù)使用的key key/keyGenerator:key的生成器,可以自定義 cacheManager:指定緩存管理器 cacheResolver:指定緩存解析器 condition:指定符合條件的數(shù)據(jù)才緩存 unless:否定緩存;當(dāng)unless指定的條件為true,方法的返回值就不會被緩存 sync:是否使用異步模式

4、@CacheEvict
清除緩存

CacheEvict:緩存清除 key:指定要清除的數(shù)據(jù) allEntries = true:指定清除這個緩存中所有的數(shù)據(jù) beforeInvocation = false:方法之前執(zhí)行清除緩存,出現(xiàn)異常不執(zhí)行 beforeInvocation = true:代表清除緩存操作是在方法運行之前執(zhí)行,無論方法是否出現(xiàn)異常,緩存都清除

5、@CachePut
保證方法被調(diào)用,又希望結(jié)果被緩存。
與@Cacheable區(qū)別在于是否每次都調(diào)用方法,常用于更新,寫入

CachePut:執(zhí)行方法且緩存方法執(zhí)行的結(jié)果 修改了數(shù)據(jù)庫的某個數(shù)據(jù),同時更新緩存; 執(zhí)行流程1)先調(diào)用目標(biāo)方法2)然后將目標(biāo)方法的結(jié)果緩存起來

6、@EnableCaching
開啟基于注解的緩存
7、keyGenerator
緩存數(shù)據(jù)時key生成策略
8、@CacheConfig
統(tǒng)一配置本類的緩存注解的屬性

三、與SpringBoot2.0整合

1、核心依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

2、Cache緩存配置

import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Method; @Configuration public class CacheConfig {/*** 自定義 Cache 的 key 生成器*/@Bean("oneKeyGenerator")public KeyGenerator getKeyGenerator (){return new KeyGenerator() {@Overridepublic Object generate(Object obj, Method method, Object... objects) {return "KeyGenerator:"+method.getName();}} ;} }

3、啟動類注解開啟Cache

@EnableCaching // 開啟Cache 緩存注解 @SpringBootApplication public class CacheApplication {public static void main(String[] args) {SpringApplication.run(CacheApplication.class,args) ;} }

4、Cache注解使用代碼

1)封裝增刪改查接口

import com.boot.cache.entity.User; public interface UserService {// 增、改、查、刪User addUser (User user) ;User updateUser (Integer id) ;User selectUser (Integer id) ;void deleteUser (Integer id); }

2)Cache注解使用案例

import com.boot.cache.entity.User; import com.boot.cache.service.UserService; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService {// 使用自定義的key生成策略// 緩存結(jié)果key:addUser::KeyGenerator:addUser@CachePut(value = "addUser",keyGenerator="oneKeyGenerator")@Overridepublic User addUser(User user) {return user ;}// 緩存結(jié)果key:updateUser::2@CachePut(value = "updateUser",key = "#result.id")@Overridepublic User updateUser(Integer id) {User user = new User() ;user.setId(id);user.setName("smile");return user;}// 緩存結(jié)果key: selectUser::3@Cacheable(cacheNames = "selectUser",key = "#id")@Overridepublic User selectUser(Integer id) {User user = new User() ;user.setId(id);user.setName("cicadaSmile");return user;}// 刪除指定key: selectUser::3@CacheEvict(value = "selectUser",key = "#id",beforeInvocation = true)@Overridepublic void deleteUser(Integer id) {} }

5、測試代碼塊

@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = CacheApplication.class) public class CacheTest {@Resourceprivate UserService userService ;// 分別測試:增、改、查、刪,四個方法@Testpublic void testAdd (){User user = new User() ;user.setId(1);user.setName("cicada");userService.addUser(user) ;}@Testpublic void testUpdate (){userService.updateUser(2) ;}@Testpublic void testSelect (){userService.selectUser(3) ;}@Testpublic void testDelete (){userService.deleteUser(3) ;} }

四、源代碼地址

GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base


總結(jié)

以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。