真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

3步輕松搞定SpringBoot緩存

作者:譚朝紅

公司專注于為企業(yè)提供網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、商城開發(fā),微信小程序開發(fā),軟件按需定制開發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。

前言

本次內(nèi)容主要介紹基于Ehcache 3.0來快速實(shí)現(xiàn)Spring Boot應(yīng)用程序的數(shù)據(jù)緩存功能。在Spring Boot應(yīng)用程序中,我們可以通過Spring Caching來快速搞定數(shù)據(jù)緩存。

3步輕松搞定Spring Boot緩存

接下來我們將介紹如何在三步之內(nèi)搞定 Spring Boot 緩存。

1. 創(chuàng)建一個(gè)Spring Boot工程

你所創(chuàng)建的Spring Boot應(yīng)用程序的maven依賴文件至少應(yīng)該是下面的樣子:



????4.0.0
????
????????org.springframework.boot
????????spring-boot-starter-parent
????????2.1.3.RELEASE
?????????
????
????com.ramostear
????cache
????0.0.1-SNAPSHOT
????cache
????Demo?project?for?Spring?Boot
????
????????1.8
????
????
????????
????????????org.springframework.boot
????????????spring-boot-starter-cache
????????
????????
????????????org.springframework.boot
????????????spring-boot-starter-web
????????
????????
????????????org.ehcache
????????????ehcache
????????
????????
????????????javax.cache
????????????cache-api
????????
????????
????????????org.springframework.boot
????????????spring-boot-starter-test
????????????test
????????
????????
????????????org.projectlombok
????????????lombok
????????
????
????
????????
????????????
????????????????org.springframework.boot
????????????????spring-boot-maven-plugin
????????????
????????
????

依賴說明:

  • spring-boot-starter-cache為Spring Boot應(yīng)用程序提供緩存支持

  • ehcache提供了Ehcache的緩存實(shí)現(xiàn)

  • cache-api 提供了基于JSR-107的緩存規(guī)范

2. 配置Ehcache緩存

現(xiàn)在,需要告訴Spring Boot去那里找緩存配置文件,這需要在Spring Boot配置文件中進(jìn)行設(shè)置:

spring.cache.jcache.config=classpath:ehcache.xml

然后使用@EnableCaching注解開啟Spring Boot應(yīng)用程序緩存功能,你可以在應(yīng)用主類中進(jìn)行操作:

package?com.ramostear.cache;
import?org.springframework.boot.SpringApplication;
import?org.springframework.boot.autoconfigure.SpringBootApplication;
import?org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public?class?CacheApplication?{
????public?static?void?main(String[]?args)?{
????????SpringApplication.run(CacheApplication.class,?args);
????}
}

接下來,需要?jiǎng)?chuàng)建一個(gè) ehcache 的配置文件,該文件放置在類路徑下,如resources目錄下:


????
????????
????
????
????????java.lang.Long
????????com.ramostear.cache.entity.Person
????????
????????????1
????????
????????
????????????
????????????????com.ramostear.cache.config.PersonCacheEventLogger
????????????????ASYNCHRONOUS
????????????????UNORDERED
????????????????CREATED
????????????????UPDATED
????????????????EXPIRED
????????????????REMOVED
????????????????EVICTED
????????????
????????
????????
????????????????2000
????????????????100
????????
????

最后,還需要定義個(gè)緩存事件監(jiān)聽器,用于記錄系統(tǒng)操作緩存數(shù)據(jù)的情況,最快的方法是實(shí)現(xiàn)CacheEventListener接口:

package?com.ramostear.cache.config;
import?org.ehcache.event.CacheEvent;
import?org.ehcache.event.CacheEventListener;
import?org.slf4j.Logger;
import?org.slf4j.LoggerFactory;
/**
?*?@author?ramostear
?*?@create-time?2019/4/7?0007-0:48
?*?@modify?by?:
?*?@since:
?*/
public?class?PersonCacheEventLogger?implements?CacheEventListener{
????private?static?final?Logger?logger?=?LoggerFactory.getLogger(PersonCacheEventLogger.class);
????@Override
????public?void?onEvent(CacheEvent?cacheEvent)?{
????????logger.info("person?caching?event?{}?{}?{}?{}",
????????????????cacheEvent.getType(),
????????????????cacheEvent.getKey(),
????????????????cacheEvent.getOldValue(),
????????????????cacheEvent.getNewValue());
????}
}

3. 使用@Cacheable注解

要讓Spring Boot能夠緩存我們的數(shù)據(jù),還需要使用@Cacheable注解對業(yè)務(wù)方法進(jìn)行注釋,告訴Spring Boot該方法中產(chǎn)生的數(shù)據(jù)需要加入到緩存中:

package?com.ramostear.cache.service;
import?com.ramostear.cache.entity.Person;
import?org.springframework.cache.annotation.Cacheable;
import?org.springframework.stereotype.Service;
/**
?*?@author?ramostear
?*?@create-time?2019/4/7?0007-0:51
?*?@modify?by?:
?*?@since:
?*/
@Service(value?=?"personService")
public?class?PersonService?{
????@Cacheable(cacheNames?=?"person",key?=?"#id")
????public?Person?getPerson(Long?id){
????????Person?person?=?new?Person(id,"ramostear","ramostear@163.com");
????????return?person;
????}
}

通過以上三個(gè)步驟,我們就完成了Spring Boot的緩存功能。接下來,我們將測試一下緩存的實(shí)際情況。

歡迎大家關(guān)注我的公種浩【程序員追風(fēng)】,文章都會(huì)在里面更新,整理的資料也會(huì)放在里面。

4. 緩存測試

為了測試我們的應(yīng)用程序,創(chuàng)建一個(gè)簡單的Restful端點(diǎn),它將調(diào)用PersonService返回一個(gè)Person對象:

package?com.ramostear.cache.controller;
import?com.ramostear.cache.entity.Person;
import?com.ramostear.cache.service.PersonService;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.http.HttpStatus;
import?org.springframework.http.ResponseEntity;
import?org.springframework.web.bind.annotation.GetMapping;
import?org.springframework.web.bind.annotation.PathVariable;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RestController;
/**
?*?@author?ramostear
?*?@create-time?2019/4/7?0007-0:54
?*?@modify?by?:
?*?@since:
?*/
@RestController
@RequestMapping("/persons")
public?class?PersonController?{
????@Autowired
????private?PersonService?personService;
????@GetMapping("/{id}")
????public?ResponseEntity?person(@PathVariable(value?=?"id")?Long?id){
????????return?new?ResponseEntity<>(personService.getPerson(id),?HttpStatus.OK);
????}
}

Person是一個(gè)簡單的POJO類:

package?com.ramostear.cache.entity;
import?lombok.AllArgsConstructor;
import?lombok.Getter;
import?lombok.NoArgsConstructor;
import?lombok.Setter;
import?java.io.Serializable;
/**
?*?@author?ramostear
?*?@create-time?2019/4/7?0007-0:45
?*?@modify?by?:
?*?@since:
?*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public?class?Person?implements?Serializable{
????private?Long?id;
????private?String?username;
????private?String?email;
}

以上準(zhǔn)備工作都完成后,讓我們編譯并運(yùn)行應(yīng)用程序。項(xiàng)目成功啟動(dòng)后,使用瀏覽器打開:http://localhost:8080/persons/1 ,你將在瀏覽器頁面中看到如下的信息:

{"id":1,"username":"ramostear","email":"ramostear@163.com"}

此時(shí)在觀察控制臺(tái)輸出的日志信息:

2019-04-07?01:08:01.001??INFO?6704?---?[nio-8080-exec-1]?
o.s.web.servlet.DispatcherServlet????????:?Completed?initialization?in?5?ms
2019-04-07?01:08:01.054??INFO?6704?---?[e?[_default_]-0]?
c.r.cache.config.PersonCacheEventLogger??:?person?caching?event?CREATED?1?
null?com.ramostear.cache.entity.Person@ba8a729

由于我們是第一次請求API,沒有任何緩存數(shù)據(jù)。因此,Ehcache創(chuàng)建了一條緩存數(shù)據(jù),可以通過CREATED看一了解到。

我們在ehcache.xml文件中將緩存過期時(shí)間設(shè)置成了1分鐘(1),因此在一分鐘之內(nèi)我們刷新瀏覽器,不會(huì)看到有新的日志輸出,一分鐘之后,緩存過期,我們再次刷新瀏覽器,將看到如下的日志輸出:

2019-04-07?01:09:28.612??INFO?6704?---?[e?[_default_]-1]
c.r.cache.config.PersonCacheEventLogger??:?person?caching?event?EXPIRED?1
com.ramostear.cache.entity.Person@a9f3c57?null
2019-04-07?01:09:28.612??INFO?6704?---?[e?[_default_]-1]?
c.r.cache.config.PersonCacheEventLogger??:?person?caching?event?CREATED?1
null?com.ramostear.cache.entity.Person@416900ce

第一條日志提示緩存已經(jīng)過期,第二條日志提示Ehcache重新創(chuàng)建了一條緩存數(shù)據(jù)。

總結(jié)

在本次案例中,通過簡單的三個(gè)步驟,講解了基于 Ehcache 的 Spring Boot 應(yīng)用程序緩存實(shí)現(xiàn)。文章內(nèi)容重在緩存實(shí)現(xiàn)的基本步驟與方法,簡化了具體的業(yè)務(wù)代碼,有興趣的朋友可以自行擴(kuò)展。

最后

歡迎大家一起交流,喜歡文章記得點(diǎn)個(gè)贊喲,感謝支持!


文章題目:3步輕松搞定SpringBoot緩存
網(wǎng)頁地址:http://weahome.cn/article/ijgdhc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部