久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot集成cache緩存的實(shí)現(xiàn)

瀏覽:38日期:2023-03-05 13:01:26
前言

日常開(kāi)發(fā)中,緩存是解決數(shù)據(jù)庫(kù)壓力的一種方案,通常用于頻繁查詢的數(shù)據(jù),例如新聞中的熱點(diǎn)新聞,本文記錄springboot中使用cache緩存。

官方文檔介紹:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-generic

工程結(jié)構(gòu)

SpringBoot集成cache緩存的實(shí)現(xiàn)

代碼編寫

pom引入依賴,引入cache緩存,數(shù)據(jù)庫(kù)使用mysql,ORM框架用jpa

<!--添加springdata-cache依賴 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency><!-- 引入ehcache支持 --><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId></dependency><!--添加springdata-jpa依賴 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--添加MySQL驅(qū)動(dòng)依賴 --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>

配置文件

server.port=10010spring.application.name=springboot-cachespring.cache.type=ehcachespring.cache.ehcache.config=classpath:/ehcache.xml

ehcache.xml

<?xml version='1.0' encoding='UTF-8'?><ehcache xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://ehcache.org/ehcache.xsd' updateCheck='false'> <!-- 磁盤緩存位置 --> <diskStore path='java.io.tmpdir'/> <!-- maxEntriesLocalHeap:堆內(nèi)存中最大緩存對(duì)象數(shù),0沒(méi)有限制 --> <!-- maxElementsInMemory: 在內(nèi)存中緩存的element的最大數(shù)目。--> <!-- eternal:elements是否永久有效,如果為true,timeouts將被忽略,element將永不過(guò)期 --> <!-- timeToIdleSeconds:發(fā)呆秒數(shù),發(fā)呆期間未訪問(wèn)緩存立即過(guò)期,當(dāng)eternal為false時(shí),這個(gè)屬性才有效,0為不限制 --> <!-- timeToLiveSeconds:總存活秒數(shù),當(dāng)eternal為false時(shí),這個(gè)屬性才有效,0為不限制 --> <!-- overflowToDisk: 如果內(nèi)存中數(shù)據(jù)超過(guò)內(nèi)存限制,是否要緩存到磁盤上 --> <!-- statistics:是否收集統(tǒng)計(jì)信息。如果需要監(jiān)控緩存使用情況,應(yīng)該打開(kāi)這個(gè)選項(xiàng)。默認(rèn)為關(guān)閉(統(tǒng)計(jì)會(huì)影響性能)。設(shè)置statistics='true'開(kāi)啟統(tǒng)計(jì) --> <!--默認(rèn)緩存無(wú)過(guò)期時(shí)間,但 600 秒內(nèi)無(wú)人訪問(wèn)緩存立即過(guò)期 --> <defaultCache maxElementsInMemory='1000' eternal='false' timeToIdleSeconds='600' timeToLiveSeconds='0' overflowToDisk='false'> </defaultCache> <!--xx業(yè)務(wù)緩存在有效的 120 秒內(nèi),如果連續(xù) 60 秒未訪問(wèn)緩存,則緩存失效。就算有訪問(wèn),也只會(huì)存活 120 秒。 --> <cache name='myCache' maxElementsInMemory='1000' eternal='false' timeToIdleSeconds='120' timeToLiveSeconds='0' overflowToDisk='false'> </cache></ehcache>

先寫一個(gè)套tb_user表的CRUD代碼

SpringBoot集成cache緩存的實(shí)現(xiàn)

@RestController@RequestMapping('/tbUser/')public class TbUserController { @Autowired private TbUserService tbUserService; //方便測(cè)試暫時(shí)改成GetMapping @GetMapping('list')// @PostMapping('list') public List<TbUser> list(TbUser entityVo) {return tbUserService.list(entityVo); } @GetMapping('get/{id}') public TbUser get(@PathVariable('id')Integer id) {return tbUserService.get(id); } //方便測(cè)試暫時(shí)改成GetMapping @GetMapping('save')// @PostMapping('save') public TbUser save(TbUser entityVo) {return tbUserService.save(entityVo); } @GetMapping('delete/{id}') public Integer delete( @PathVariable('id') Integer id) {return tbUserService.delete(id); }}

opjo實(shí)體類要實(shí)現(xiàn)序列化

@Entity@Table(name = 'tb_user')@Datapublic class TbUser implements Serializable { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Integer id;//表id private String username;//用戶名 private String password;//密碼 private Date created;//創(chuàng)建時(shí)間 private Integer descriptionId;//關(guān)聯(lián)詳情id}

serviceImpl中,使用注解來(lái)開(kāi)啟緩存

@Service@Transactional@CacheConfig(cacheNames = {'myCache'})public class TbUserServiceImpl implements TbUserService{ @PersistenceContext private EntityManager em; @Autowired private TbUserRepository tbUserRepository; //@Cacheable緩存數(shù)據(jù):key為userList,value為返回值List<TbUser> @Cacheable(key = '’userList’') @Override public List<TbUser> list(TbUser entityVo) {System.out.println('獲取list用戶列表緩存數(shù)據(jù),'+new Date());return tbUserRepository.findAll(Example.of(entityVo)); } //@Cacheable緩存數(shù)據(jù):key為參數(shù)id,value為返回值TbUser @Cacheable(key = '#id') @Override public TbUser get(Integer id) {System.out.println('獲取數(shù)據(jù)緩存,key:'+id);Optional<TbUser> optionalE = tbUserRepository.findById(id);if (!optionalE.isPresent()) { throw new RuntimeException('ID不存在!');}return optionalE.get(); } //@CachePut緩存新增的或更新的數(shù)據(jù)到緩存,其中緩存的名稱為people,數(shù)據(jù)的key是person的id @CachePut(key = '#entityVo.id') // @CacheEvict從緩存中刪除key為參數(shù)userList的數(shù)據(jù) @CacheEvict(key = '’userList’') @Override public TbUser save(TbUser entityVo) {System.out.println('新增/更新緩存,key:'+entityVo.getId());//entityVo傳啥存啥,會(huì)全部更新return tbUserRepository.save(entityVo); } //清空所有緩存 @CacheEvict(allEntries=true) @Override public Integer delete(Integer id) {System.out.println('清空所有緩存');tbUserRepository.deleteById(id);return id; }}

效果演示

http://localhost:10010/tbUser/save?id=2&username=李四

調(diào)用save方法,key為2,value為當(dāng)前tbUser對(duì)象的數(shù)據(jù)被緩存下來(lái)

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

http://localhost:10010/tbUser/get/2

當(dāng)我們調(diào)用get方法時(shí),直接獲取緩存數(shù)據(jù),控制臺(tái)啥也不打印,連serviceImpl的get方法都不進(jìn)去(可以打斷點(diǎn)調(diào)試)

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

http://localhost:10010/tbUser/save?id=2&username=王五

當(dāng)我們?cè)俅握{(diào)用save方法更新username時(shí),緩存數(shù)據(jù)也被更新

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

http://localhost:10010/tbUser/get/2

再次調(diào)用get接口,直接返回緩存數(shù)據(jù),后臺(tái)也是方法都不進(jìn)去,啥也不打印

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

http://localhost:10010/tbUser/delete/2

調(diào)用delete接口,刪除數(shù)據(jù),同時(shí)刪除緩存

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

再次調(diào)用get接口,發(fā)現(xiàn)緩存數(shù)據(jù)被清除,查詢數(shù)據(jù)庫(kù)

SpringBoot集成cache緩存的實(shí)現(xiàn)

http://localhost:10010/tbUser/list

首次調(diào)用list接口,key為userList的,value為用戶集合數(shù)據(jù)被緩存下來(lái),再次調(diào)用直接返回緩存數(shù)據(jù)

SpringBoot集成cache緩存的實(shí)現(xiàn)

SpringBoot集成cache緩存的實(shí)現(xiàn)

當(dāng)調(diào)用save接口,數(shù)據(jù)更新,刪除key為userList的緩存,再次調(diào)用list時(shí),重新查庫(kù)并設(shè)置緩存

SpringBoot集成cache緩存的實(shí)現(xiàn)

我們配置了緩存發(fā)呆時(shí)間,當(dāng)120秒內(nèi)未使用該緩存,立即過(guò)期,一直用就會(huì)一直存在

我們先同時(shí)訪問(wèn)兩個(gè)接口list、get,list接口2分鐘后再次訪問(wèn),get接口不能超過(guò)2分鐘是不是訪問(wèn)一下,結(jié)果如預(yù)期

SpringBoot集成cache緩存的實(shí)現(xiàn)

PS:原先使用了這個(gè)jar包,有報(bào)錯(cuò)

<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> </dependency>

后面改成用上面“代碼編寫”里pom中引的jnet.sf.ehcache下面的ar

后記

緩存除了能緩解數(shù)據(jù)庫(kù)壓力,還能做用戶登錄狀態(tài)控制,例如:用戶登錄成功后cookie中保存頒發(fā)的token令牌設(shè)置永不過(guò)期,緩存存活時(shí)間也設(shè)置永不過(guò)期,發(fā)呆時(shí)間設(shè)置1天,這樣只有用戶在1天內(nèi)有訪問(wèn)緩存接口,那他就可以一直保留登錄狀態(tài),直至有其他業(yè)務(wù)將token或者緩存清掉。

springboot使用cache緩存暫時(shí)先記錄到這,后續(xù)有空再進(jìn)行補(bǔ)充。

代碼開(kāi)源

代碼已經(jīng)開(kāi)源、托管到我的GitHub、碼云:

GitHub:https://github.com/huanzi-qch/springBoot

碼云:https://gitee.com/huanzi-qch/springBoot

到此這篇關(guān)于SpringBoot集成cache緩存的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot cache緩存內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 平泉县| 丰原市| 贵溪市| 林州市| 馆陶县| 武义县| 沅江市| 郧西县| 云浮市| 山东省| 夏邑县| 镇江市| 克什克腾旗| 临朐县| 云林县| 太康县| 抚州市| 西吉县| 商丘市| 长寿区| 普陀区| 昭平县| 和静县| 广饶县| 平江县| 余干县| 西华县| 巢湖市| 夏津县| 陆丰市| 永胜县| 福海县| 斗六市| 南江县| 鹤山市| 台山市| 葫芦岛市| 南京市| 贵南县| 蒲江县| 平潭县|