亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Springboot使用cache緩存過程代碼實例

瀏覽:3日期:2023-05-08 13:58:11

1.pom.xml

<!-- Ehcache 坐標 --><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId></dependency>

2.ehcache.xml

<?xml version='1.0' encoding='UTF-8'?><ehcache> <diskStore path='java.io.tmpdir'/> <!--defaultCache:echcache的默認緩存策略 --> <defaultCache maxElementsInMemory='10000' eternal='false' timeToIdleSeconds='120' timeToLiveSeconds='120' maxElementsOnDisk='10000000' diskExpiryThreadIntervalSeconds='120' memoryStoreEvictionPolicy='LRU'> <persistence strategy='localTempSwap'/> </defaultCache> <!-- maxElementsInMemory設置成1,overflowToDisk設置成true,只要有一個緩存元素,就直接存到硬盤上去 eternal設置成true,代表對象永久有效 maxElementsOnDisk設置成0 表示硬盤中最大緩存對象數(shù)無限大 diskPersistent設置成true表示緩存虛擬機重啟期數(shù)據(jù) --> <cache name='usercache' maxElementsInMemory='1' eternal='true' overflowToDisk='true' maxElementsOnDisk='0' diskPersistent='true'><!-- <persistence strategy='localTempSwap'/>--> <!--不能和diskPersistent 同時存在--> </cache>

diskStore是物理文件的存儲路徑,

cache標簽中的name是多cache時區(qū)分的唯一標識, 和程序中初始化方法getCache('***')參數(shù)一致。<br>緩存參數(shù)和本地數(shù)據(jù)持久化存儲需自行配置

3.application.yml

spring: cache: ehcache: config: classpath:/ehcache.xml

4.啟動類添加

@EnableCaching

import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching; @EnableCaching@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

5.springcloud 中使用cache

import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.ClassPathResource;import org.springframework.stereotype.Component; import java.io.IOException; /** * @Author: Peacock__ * @Date: 2019/6/14 17:30 */@Componentpublic class CacheService { @Autowired private CacheManager cacheManager; /** * 從緩存中獲取數(shù)據(jù) * @return * @throws IOException */ public String getCache() throws IOException { String res = ''; Cache cache = cacheManager.getCache('usercache'); if(cache != null){ Element element = cache.get('name'); if(element != null){Object objectValue = element.getObjectValue();res = (String) objectValue; } } return res; } /** * 數(shù)據(jù)存入緩存 * @param data * @throws IOException */ public void putCache(String data) throws IOException { //若cacheManager被關閉,則重新創(chuàng)建 if(cacheManager == null || cacheManager.getStatus().intValue() != 1){ cacheManager = new CacheManager(new ClassPathResource('ehcache.xml').getInputStream()); } Cache cache = cacheManager.getCache('usercache'); //處理成要緩存的數(shù)據(jù) //存入緩存(注意:需要保證存入緩存的數(shù)據(jù)都是可序列化的) cache.put(new Element('name', data)); /** * ehcache和其它緩存類似,需要flush或shutdown后才會持久化到磁盤。 * 會生成.data 的數(shù)據(jù)文件和 .index 的索引文件,方便重啟恢復。 * ehcache恢復數(shù)據(jù)是根據(jù).index索引文件來進行數(shù)據(jù)恢復的。 * 當程序再次啟動的時候,ehcache的一個方法會將.data文件和.index文件的修改時間進行比較,如果不符合直接將.index文件刪除。 */ //將所有緩存項從內存刷新到磁盤存儲,并從DiskStore刷新到磁盤。// cache.flush(); //更新.index文件// cacheManager.shutdown(); }}

6.controller層

import java.io.IOException; @RestControllerpublic class AppController{ @Autowired private CacheService cacheService; @RequestMapping('/setName') public String setName() { try { cacheService.putCache( 'heshan'); } catch (IOException e) { e.printStackTrace(); } return 'yes'; } @RequestMapping('/getName') public String getName() { String res = null; try { res = cacheService.getCache( ); } catch (IOException e) { e.printStackTrace(); } return res; }}

結果:

Springboot使用cache緩存過程代碼實例Springboot使用cache緩存過程代碼實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關文章:
主站蜘蛛池模板: 青青久操 | 国产精品公开免费视频 | 成年视频xxxxx在线入口 | 爆操御姐 | 欧美成人精品手机在线观看 | 欧美片能看的一级毛片 | 麻豆果冻传媒一卡二卡 | 国产在线观看高清不卡 | 久久国产精品视频 | 九一国产精品视频 | 中国人免费观看高清在线观看二区 | 国产高清视频免费 | 国产成人一区二区三区视频免费 | 免费看的黄视频 | 日韩欧美一区二区三区免费看 | 久久精品国产一区二区三区日韩 | 性大片免费观看yy | 91丝瓜视频最新版 | 久久精品在线免费观看 | 久久亚洲伊人成综合人影院 | 91短视频社区在线观看 | 亚洲精品色图 | 亚洲欧美在线免费 | 午夜精品久久久久久久久 | 黄色aaa| 日本黄色大片视频 | 欧美一级片黄色 | 国产一国产一级毛片视频 | 晚上睡不着偷偷看b站免费是视频 | 青青草视频破解版 | 国产欧美在线不卡 | 国产a级特黄的片子视频免费 | 瑟瑟网站免费 | 国产高清视频网站 | 亚洲欧美日韩成人一区在线 | 日本亚洲国产 | 亚洲制服丝袜第一页 | 国产在亚洲线视频观看 | 爽妇综合网 | 欧美精品第1页在线播放 | 日韩在线你懂的 |