5.2.2 基於註解的Redis緩存實現java
在Spring Boot默認緩存管理的基礎上引入Redis緩存組件,使用基於註解的方式講解Spring Boot整合Redis緩存的具體實現 redis
(1)添加Spring Data Redis依賴啓動器。在pom.xml文件中添加Spring Data Redis依賴啓動器 spring
xml數據庫
<dependency>瀏覽器
<groupId>org.springframework.boot</groupId>緩存
<artifactId>spring-boot-starter-data-redis</artifactId>服務器
</dependency>less
當咱們添加進redis相關的啓動器以後, SpringBoot會使用`RedisCacheConfigratioin`當作生效的自動配置類進行緩存相關的自動裝配,容器中使用的緩存管理器是spring-boot
`RedisCacheManager`, 這個緩存管理器建立的Cache爲 `RedisCache`, 進而操控redis進行數據的緩存工具
(2)Redis服務鏈接配置
properties
Redis服務地址
spring.redis.host=127.0.0.1
Redis服務器鏈接端口
spring.redis.port=6379
Redis服務器鏈接密碼(默認爲空)
spring.redis.password=
(3)對CommentService類中的方法進行修改使用@Cacheable、@CachePut、@CacheEvict三個註解定製緩存管理,分別進行緩存存儲、緩存更新和緩存刪除的演示
```java
@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
@Cacheable(cacheNames = "comment",unless = "#result==null")
public Comment findCommentById(Integer id){
Optional<Comment> comment = commentRepository.findById(id);
if(comment.isPresent()){
Comment comment1 = comment.get();
return comment1;
}
return null;
}
@CachePut(cacheNames = "comment",key = "#result.id")
public Comment updateComment(Comment comment) {
commentRepository.updateComment(comment.getAuthor(), comment.getaId());
return comment;
}
@CacheEvict(cacheNames = "comment")
public void deleteComment(int comment_id) {
commentRepository.deleteById(comment_id);
}
}
```
以上 使用@Cacheable、@CachePut、@CacheEvict註解在數據查詢、更新和刪除方法上進行了緩存管理。
其中,查詢緩存@Cacheable註解中沒有標記key值,將會使用默認參數值comment_id做爲key進行數據保存,在進行緩存更新時必須使用一樣的key;同時在查詢緩存@Cacheable註解中,定義了「unless = "#result==null"」表示查詢結果爲空不進行緩存
(4) 基於註解的Redis查詢緩存測試
![image-20191231141712756](./images/image-20191231141712756.png)
能夠看出,查詢用戶評論信息Comment時執行了相應的SQL語句,可是在進行緩存存儲時出現了IllegalArgumentException非法參數異常,提示信息要求對應Comment實體類必須實現序列化(「DefaultSerializer requires a Serializable payload but received an object of type」)。
(5)將緩存對象實現序列化。
<img src="./images/image-20191231141845520.png" alt="image-20191231141845520" style="zoom:67%;" />
(6)再次啓動測試
訪問「http://localhost:8080/findCommentById?id=1」查詢id爲1的用戶評論信息,並重復刷新瀏覽器查詢同一條數據信息,查詢結果
![image-20191231142056554](./images/image-20191231142056554.png)
查看控制檯打印的SQL查詢語句
![image-20191231142114105](./images/image-20191231142114105.png)
還能夠打開Redis客戶端可視化管理工具Redis Desktop Manager鏈接本地啓用的Redis服務,查看具體的數據緩存效果
![image-20191231142353290](./images/image-20191231142353290.png)
執行findById()方法查詢出的用戶評論信息Comment正確存儲到了Redis緩存庫中名爲comment的名稱空間下。其中緩存數據的惟一標識key值是以「名稱空間comment::+參數值(comment::1)」的字符串形式體現的,而value值則是通過JDK默認序列格式化後的HEX格式存儲。這種JDK默認序列格式化後的數據顯然不方便緩存數據的可視化查看和管理,因此在實際開發中,一般會自定義數據的序列化格式
(7) 基於註解的Redis緩存更新測試。
先經過瀏覽器訪問「http://localhost:8080/update/1/shitou」更新id爲1的評論做者名爲shitou;
接着,繼續訪問「http://localhost:8080/get/1」查詢id爲1的用戶評論信息
<img src="./images/image-20191231144329586.png" alt="image-20191231144329586" style="zoom:67%;" />
![image-20191231144533512](./images/image-20191231144533512.png)
能夠看出,執行updateComment()方法更新id爲1的數據時執行了一條更新SQL語句,後續調用findById()方法查詢id爲1的用戶評論信息時沒有執行查詢SQL語句,且瀏覽器正確返回了更新後的結果,代表@CachePut緩存更新配置成功
(8)基於註解的Redis緩存刪除測試
經過瀏覽器訪問「http://localhost:8080/deleteComment?id=1」刪除id爲1的用戶評論信息;
![image-20191231145925740](./images/image-20191231145925740.png)
執行deleteComment()方法刪除id爲1的數據後查詢結果爲空,以前存儲在Redis數據庫的comment相關數據也被刪除,代表@CacheEvict緩存刪除成功實現
經過上面的案例能夠看出,使用基於註解的Redis緩存實現只須要添加Redis依賴並使用幾個註解能夠實現對數據的緩存管理。另外,還能夠在Spring Boot全局配置文件中配置Redis有效期,示例代碼以下:
properties
對基於註解的Redis緩存數據統一設置有效期爲1分鐘,單位毫秒
spring.cache.redis.time-to-live=60000
上述代碼中,在Spring Boot全局配置文件中添加了「spring.cache.redis.time-to-live」屬性統一配置Redis數據的有效期(單位爲毫秒),但這種方式相對來講不夠靈活
剛學了拉勾教育的《Java工程師高薪訓練營》,看到剛學到的點就回答了。但願拉勾能給我推到想去的公司,目標:字節!!