當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文源碼
GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
一、Cache緩存簡介
從Spring3開始定義Cache和CacheManager接口來統一不同的緩存技術;
Cache接口為緩存的組件規范定義,包含緩存的各種操作集合;
Cache接口下Spring提供了各種緩存的實現;
如RedisCache,EhCacheCache ,ConcurrentMapCache等;
二、核心API
1、Cache緩存接口
定義緩存操作。實現有:RedisCache、EhCacheCache、ConcurrentMapCache等
2、CacheManager
緩存管理器,管理各種緩存(cache)組件
3、@Cacheable 主要針對方法配置,能夠根據方法的請求參數對其進行緩存
4、@CacheEvict
清除緩存
5、@CachePut
保證方法被調用,又希望結果被緩存。
與@Cacheable區別在于是否每次都調用方法,常用于更新,寫入
6、@EnableCaching
開啟基于注解的緩存
7、keyGenerator
緩存數據時key生成策略
8、@CacheConfig
統一配置本類的緩存注解的屬性
三、與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生成策略// 緩存結果key:addUser::KeyGenerator:addUser@CachePut(value = "addUser",keyGenerator="oneKeyGenerator")@Overridepublic User addUser(User user) {return user ;}// 緩存結果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;}// 緩存結果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
總結
以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Firebug 1.7正式版发布,支持F
- 下一篇: 人生何为苦何为乐何为幸福