SpringBoot集成SpringCache的過程詳解-創(chuàng)新互聯(lián)

這篇文章主要講解了“SpringBoot集成SpringCache的過程詳解”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot集成SpringCache的過程詳解”吧!

創(chuàng)新互聯(lián)長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為湘鄉(xiāng)企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),湘鄉(xiāng)網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

一、關(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實(shí)現(xiàn),主要是@Cacheable、@CachePut、@CacheEvict、@CacheConfig、@Caching等注解來實(shí)現(xiàn)。

@Cacheable:作用于方法上,用于對于方法返回結(jié)果進(jìn)行緩存,如果已經(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 //開啟緩存@SpringBootApplicationpublic class SpringbootApplication {  public static void main(String[] args) {    SpringApplication.run(SpringbootApplication.class, args);  }}

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

@Slf4j@Servicepublic 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進(jìn)行測試。

@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)進(jìn)行測試,多次調(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

感謝各位的閱讀,以上就是“SpringBoot集成SpringCache的過程詳解”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對SpringBoot集成SpringCache的過程詳解這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

分享文章:SpringBoot集成SpringCache的過程詳解-創(chuàng)新互聯(lián)
瀏覽地址:http://www.muchs.cn/article2/dcdooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、Google、App設(shè)計(jì)、建站公司、微信小程序定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)