亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot集成Spring Cache過程詳解

 更新時間:2019年10月24日 08:57:24   作者:楊小格子  
這篇文章主要介紹了Spring Boot集成Spring Cache過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一、關(guān)于Spring Cache

緩存在現(xiàn)在的應(yīng)用中越來越重要,

Spring從3.1開始定義了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口來統(tǒng)一不同的緩存技術(shù),并支持使用JCache(JSR-107)注解簡化我們開發(fā)。

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

  • @Cacheable:作用于方法上,用于對于方法返回結(jié)果進行緩存,如果已經(jīng)存在該緩存,則直接從緩存中獲取,緩存的key可以從入?yún)⒅兄付?,緩存的value為方法返回值。
  • @CachePut:作用于方法上,無論是否存在該緩存,每次都會重新添加緩存,緩存的key可以從入?yún)⒅兄付ǎ彺娴膙alue為方法返回值,常用作于更新。
  • @CacheEvict:作用于方法上,用于清除緩存。
  • @CacheConfig:作用在類上,統(tǒng)一配置本類的緩存注解的屬性。
  • @Caching:作用于方法上,用于一次性設(shè)置多個緩存。
  • @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);
  }

}

這樣就可以使用注解來操作緩存了,創(chuàng)建CacheService類,其中dataMap的Map存儲數(shù)據(jù),省去了數(shù)據(jù)庫的操作。

@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);
      }
    }
   };

  // 獲取數(shù)據(jù)
  @Cacheable(value = "cache", key = "'user:' + #id")
  public User get(int id){
    log.info("通過id{}查詢獲取", id);
    return dataMap.get(id);
  }

  // 更新數(shù)據(jù)
  @CachePut(value = "cache", key = "'user:' + #id")
  public User set(int id, User u){
    log.info("更新id{}數(shù)據(jù)", id);
    dataMap.put(id, u);
    return u;
   }
   
  //刪除數(shù)據(jù)
  @CacheEvict(value = "cache", key = "'user:' + #id")
  public User del(int id){
    log.info("刪除id{}數(shù)據(jù)", id);
    dataMap.remove(id);
    return u;
  }

}

get方法模擬查詢,@Cacheable用于添加緩存,set方法用于修改,@CachePut更新緩存,del方法用于刪除數(shù)據(jù), @CacheEvict刪除緩存。需要注意的是,注解的value表示緩存分類,并不是指緩存的對象值。

然后在創(chuàng)建CacheApi,用于調(diào)用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)進行測試,多次調(diào)用查詢,可以看到, CacheService的get方法,對于同一id僅僅執(zhí)行一遍。然后再調(diào)用更新,再次get時,即可發(fā)現(xiàn)數(shù)據(jù)已經(jīng)更新,而調(diào)用del,則可以清除緩存,再次查詢又會調(diào)用方法。

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot集成vue的開發(fā)解決方案

    SpringBoot集成vue的開發(fā)解決方案

    這篇文章主要介紹了SpringBoot集成vue的開發(fā)解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解

    springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的

    今天小編就為大家分享一篇關(guān)于springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • SpringCloud基于RestTemplate微服務(wù)項目案例解析

    SpringCloud基于RestTemplate微服務(wù)項目案例解析

    這篇文章主要介紹了SpringCloud基于RestTemplate微服務(wù)項目案例,在寫SpringCloud搭建微服務(wù)之前,先搭建一個不通過springcloud只通過SpringBoot和Mybatis進行模塊之間通訊,通過一個案例給大家詳細說明,需要的朋友可以參考下
    2022-05-05
  • idea創(chuàng)建springboot項目,Application.java不能運行問題及解決

    idea創(chuàng)建springboot項目,Application.java不能運行問題及解決

    這篇文章主要介紹了idea創(chuàng)建springboot項目,Application.java不能運行問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java中的ReentrantLock解讀

    Java中的ReentrantLock解讀

    這篇文章主要介紹了Java中的ReentrantLock解讀,ReentantLock是java中重入鎖的實現(xiàn),一次只能有一個線程來持有鎖,包含三個內(nèi)部類,Sync、NonFairSync、FairSync,需要的朋友可以參考下
    2023-09-09
  • 在spring-boot工程中添加spring mvc攔截器

    在spring-boot工程中添加spring mvc攔截器

    這篇文章主要介紹了在spring-boot工程中添加spring mvc攔截器,Spring MVC的攔截器(Interceptor)不是Filter,同樣可以實現(xiàn)請求的預(yù)處理、后處理。,需要的朋友可以參考下
    2019-06-06
  • JAVA一個快速排序?qū)崿F(xiàn)代碼

    JAVA一個快速排序?qū)崿F(xiàn)代碼

    排序有哪幾種方法?請列舉。并用JAVA實現(xiàn)一個快速排序.,需要的朋友可以參考下
    2017-02-02
  • SpringBoot+VUE實現(xiàn)前后端分離的實戰(zhàn)記錄

    SpringBoot+VUE實現(xiàn)前后端分離的實戰(zhàn)記錄

    這篇文章主要介紹了SpringBoot+VUE實現(xiàn)前后端分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • spring jdbctemplate的用法小結(jié)

    spring jdbctemplate的用法小結(jié)

    jdbcTemplate是spring框架中提供的一個對象,是對原始繁雜的jdbc 對象的簡單封裝,本文通過實例代碼介紹spring jdbctemplate的用法小結(jié),需要的朋友可以參考下
    2023-04-04
  • Java實現(xiàn)動態(tài)數(shù)字時鐘

    Java實現(xiàn)動態(tài)數(shù)字時鐘

    這篇文章主要為大家詳細介紹了Java實現(xiàn)動態(tài)數(shù)字時鐘,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論