日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

注解方式使用 Redis 缓存

發布時間:2024/4/13 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 注解方式使用 Redis 缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用緩存有兩個前置步驟

  • 在 pom.xml 引入依賴

  • <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合redis --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- springboot測試 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>

    在啟動類上加注解 @EnableCaching

    @SpringBootApplication @EnableCaching public class RedisApplication {public static void main(String[] args) {SpringApplication.run(RedisApplication.class, args);} }

    常用的注解有以下幾個@Cacheable、@CachePut、@CacheEvict

    添加緩存

    緩存的key就是配置在注解里面的值product::123,值是你方法的返回值,如果沒有返回值,值就是org.springframework.cache.support.NullValue

    在需要加緩存的方法上添加注解 @Cacheable(cacheNames = "product", key = "123"),

    cacheNames 和 key 都必須填,如果不填 key ,默認的 key 是當前的方法名,更新緩存時會因為方法名不同而更新失敗。

    如在訂單列表上加緩存

    @RequestMapping(value = "/list", method = RequestMethod.GET)@Cacheable(cacheNames = "product", key = "123")public List<Product> list() {List<Product> products=new ArrayList<>();for (int i = 0; i < 3; i++) {Product product=new Product();product.setId(i+1);product.setName("第"+(i+1)+"件商品");product.setPrice((double) ((i+1)*100));products.add(product);}return products;}

    可能會報錯,原因是對象未序列化。讓對象實現 Serializable 方法即可。

    @Data @NoArgsConstructor @AllArgsConstructor public class Product implements Serializable {private Integer id;private String name;private Double price; }

    重啟項目訪問訂單列表,在 rdm 里查看 Redis 緩存,有 product::123 說明緩存成功。

    更新緩存

    在需要更新緩存的方法上加注解: @CachePut(cacheNames = "product", key = "123")

    注意

  • cacheNames 和 key 要跟 @Cacheable() 里的一致,才會正確更新。

  • @CachePut() 和 @Cacheable() 注解的方法返回值要一致

  • 刪除緩存

    在需要刪除緩存的方法上加注解:@CacheEvict(cacheNames = "product", key = "123"),執行完這個方法之后會將 Redis 中對應的記錄刪除。

    其他常用功能

  • cacheNames 也可以統一寫在類上面, @CacheConfig(cacheNames = "product") ,具體的方法上就不用寫啦。

  • @RestController @CacheConfig(cacheNames = "product") public class ProdectController {}

    Key 也可以動態設置為方法的參數

    @GetMapping("/detail")@Cacheable(cacheNames = "product", key = "#id")public Product detail(@RequestParam("id") Integer id){if (id==1){return new Product(1,"電冰箱",20d);}else if (id==2){return new Product(2,"洗衣機",30d);}else {return new Product(3,"彩電",40d);}}
  • 如果參數是個對象,也可以設置對象的某個屬性為 key。比如其中一個參數是 user 對象,key 可以寫成 key="#user.id"

  • 緩存還可以設置條件。

    設置當 openid 的長度大于3時才緩存

  • @GetMapping("/detailOnCondition")@Cacheable(cacheNames = "product", key = "#id", condition = "#id > 2")public void detail(@RequestParam("id") String id){System.out.println("id是"+id);}

    還可以指定 unless即條件不成立時緩存。#result 代表返回值,意思是當返回碼不等于 0 時不緩存,也就是等于 0 時才緩存。

    @GetMapping("/detailOnConditionAndUnless")@Cacheable(cacheNames = "product", key = "#id", condition = "#id > 2", unless = "#result!= 0")public Integer detailOnConditionAndUnless(@RequestParam("id") Integer id){if (id==3){return 0;}else {return 1;}}

    ?

    總結

    以上是生活随笔為你收集整理的注解方式使用 Redis 缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。