SpringBoot系列:Spring Boot集成Spring Cache

  • 2019 年 10 月 23 日
  • 筆記

一、關於Spring Cache

快取在現在的應用中越來越重要,
Spring從3.1開始定義了org.springframework.cache.Cache和org.springframework.cache.CacheManager介面來統一不同的快取技術,並支援使用JCache(JSR-107)註解簡化我們開發。

通過SpringCache,可以快速嵌入自己的Cache實現,主要是@Cacheable、@CachePut、@CacheEvict、@CacheConfig、@Caching等註解來實現。

  • @Cacheable:作用於方法上,用於對於方法返回結果進行快取,如果已經存在該快取,則直接從快取中獲取,快取的key可以從入參中指定,快取的value為方法返回值。
  • @CachePut:作用於方法上,無論是否存在該快取,每次都會重新添加快取,快取的key可以從入參中指定,快取的value為方法返回值,常用作於更新。
  • @CacheEvict:作用於方法上,用於清除快取。
  • @CacheConfig:作用在類上,統一配置本類的快取註解的屬性。
  • @Caching:作用於方法上,用於一次性設置多個快取。
  • @EnableCaching:作用於類上,用於開啟註解功能。

二、演示示例

欲使用Spring Cache,需要先引入Spring Cache的依賴。

<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <!--Spring Cache依賴-->  <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-cache</artifactId>  </dependency>

然後在啟動類上,我們需要使用@EnableCaching來聲明開啟快取。

@EnableCaching //開啟快取  @SpringBootApplication  public class SpringbootApplication {        public static void main(String[] args) {          SpringApplication.run(SpringbootApplication.class, args);      }    }

這樣就可以使用註解來操作快取了,創建CacheService類,其中dataMap的Map存儲數據,省去了資料庫的操作。

@Slf4j  @Service  public class CacheService {        private Map<Integer, User> dataMap = new HashMap <Integer, User>(){          {              for (int i = 1; i < 100 ; i++) {                  User u = new User("code" + i, "name" + i);                  put(i, u);              }          }       };        // 獲取數據      @Cacheable(value = "cache", key = "'user:' + #id")      public User get(int id){          log.info("通過id{}查詢獲取", id);          return dataMap.get(id);      }        // 更新數據      @CachePut(value = "cache", key = "'user:' + #id")      public User set(int id, User u){          log.info("更新id{}數據", id);          dataMap.put(id, u);          return u;       }        //刪除數據      @CacheEvict(value = "cache", key = "'user:' + #id")      public User del(int id){          log.info("刪除id{}數據", id);          dataMap.remove(id);          return u;      }    }

get方法模擬查詢,@Cacheable用於添加快取,set方法用於修改,@CachePut更新快取,del方法用於刪除數據, @CacheEvict刪除快取。需要注意的是,註解的value表示快取分類,並不是指快取的對象值。

然後在創建CacheApi,用於調用CacheService進行測試。

@RestController  @RequestMapping("cache")  public class CacheApi {        @Autowired      private CacheService cacheService;        @GetMapping("get")      public User  get(@RequestParam int id){          return cacheService.get(id);      }        @PostMapping("set")      public User  set(@RequestParam int id, @RequestParam String code, @RequestParam String name){          User u = new User(code, name);          return cacheService.set(id, u);      }        @DeleteMapping("del")      public void  del(@RequestParam int id){          cacheService.del(id);      }    }

然後我們打開swagger-ui介面(http://localhost:10900/swagger-ui.html)進行測試,多次調用查詢,可以看到, CacheService的get方法,對於同一id僅僅執行一遍。然後再調用更新,再次get時,即可發現數據已經更新,而調用del,則可以清除快取,再次查詢又會調用方法。

源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

Exit mobile version