欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringBoot中如何使用EhCache-創(chuàng)新互聯(lián)

這篇文章給大家介紹SpringBoot中如何使用EhCache,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

為同安等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及同安網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、同安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

一、EhCache使用演示

EhCache是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),Hibernate中的默認(rèn)Cache就是使用的EhCache。

本章節(jié)示例是在Spring Boot集成Spring Cache的源碼基礎(chǔ)上進(jìn)行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用EhCache作為緩存,我們先引入相關(guān)依賴(lài)。

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId></dependency><!--ehcache依賴(lài)--><dependency>  <groupId>net.sf.ehcache</groupId>  <artifactId>ehcache</artifactId></dependency><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-cache</artifactId></dependency>

然后創(chuàng)建EhCache的配置文件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">  <!--    磁盤(pán)存儲(chǔ):將緩存中暫時(shí)不使用的對(duì)象,轉(zhuǎn)移到硬盤(pán),類(lèi)似于Windows系統(tǒng)的虛擬內(nèi)存    path:指定在硬盤(pán)上存儲(chǔ)對(duì)象的路徑    path可以配置的目錄有:    user.home(用戶(hù)的家目錄)    user.dir(用戶(hù)當(dāng)前的工作目錄)    java.io.tmpdir(默認(rèn)的臨時(shí)目錄)    ehcache.disk.store.dir(ehcache的配置目錄)    絕對(duì)路徑(如:d:\\ehcache)    查看路徑方法:String tmpDir = System.getProperty("java.io.tmpdir");   -->  <diskStore path="java.io.tmpdir" />  <!--    defaultCache:默認(rèn)的緩存配置信息,如果不加特殊說(shuō)明,則所有對(duì)象按照此配置項(xiàng)處理    maxElementsInMemory:設(shè)置了緩存的上限,最多存儲(chǔ)多少個(gè)記錄對(duì)象    eternal:代表對(duì)象是否永不過(guò)期 (指定true則下面兩項(xiàng)配置需為0無(wú)限期)    timeToIdleSeconds:較大的發(fā)呆時(shí)間 /秒    timeToLiveSeconds:較大的存活時(shí)間 /秒    overflowToDisk:是否允許對(duì)象被寫(xiě)入到磁盤(pán)    說(shuō)明:下列配置自緩存建立起600秒(10分鐘)有效 。    在有效的600秒(10分鐘)內(nèi),如果連續(xù)120秒(2分鐘)未訪問(wèn)緩存,則緩存失效。    就算有訪問(wèn),也只會(huì)存活600秒。   -->  <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"         timeToLiveSeconds="600" overflowToDisk="true" />  <cache name="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"      timeToLiveSeconds="600" overflowToDisk="true" /></ehcache>

然后SpringBoot配置文件中,指明緩存類(lèi)型并聲明Ehcache配置文件的位置。

server: port: 10900spring: profiles:  active: dev cache:  type: ehcache  ehcache:   config: classpath:/ehcache.xml

這樣就可以開(kāi)始使用Ehcache了,測(cè)試代碼與Spring Boot集成Spring Cache一致。

SpringBoot啟動(dòng)類(lèi),@EnableCaching開(kāi)啟Spring Cache緩存功能。

@EnableCaching@SpringBootApplicationpublic class SpringbootApplication {  public static void main(String[] args) {    String tmpDir = System.getProperty("java.io.tmpdir");    System.out.println("臨時(shí)路徑:" + tmpDir);    SpringApplication.run(SpringbootApplication.class, args);  }}

CacheApi接口調(diào)用類(lèi),方便調(diào)用進(jìn)行測(cè)試。

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

CacheService緩存業(yè)務(wù)處理類(lèi),添加緩存,更新緩存和刪除。

@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("通過(guò)id{}查詢(xún)獲取", 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 void del(int id){    log.info("刪除id{}數(shù)據(jù)", id);    dataMap.remove(id);  }  }

關(guān)于SpringBoot中如何使用EhCache就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞標(biāo)題:SpringBoot中如何使用EhCache-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://aaarwkj.com/article36/pgppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作標(biāo)簽優(yōu)化全網(wǎng)營(yíng)銷(xiāo)推廣、做網(wǎng)站、網(wǎng)站排名品牌網(wǎng)站制作

廣告

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

外貿(mào)網(wǎng)站制作
久久精品国产亚洲av亚| 欧美日韩伦理片在线观看| 国产精品麻豆一区二区三区| 91一区二区三区在线| 午夜精品国产日韩欧美在线| 国产精品亚洲欧美在线| 亚洲国产日韩欧美综合久久| 开心五月六月婷婷在线| 国产91在线视频播放| 毛片精品一区二区二区三区 | 国产精品亚洲av在线| 日韩成人午夜视频在线| 一区二区三区在线观看淫| 尤物资源视频在线观看| 四虎在线观看免费资源| 国产一区二区精品久久岳 | 国产丝袜美腿在线观看| 蜜桃午夜精品一区二区三区| 美味人妻手机在线观看| 四虎永久精品在线视频| 深夜视频国产在线观看| 四虎精品国产一区二区三区| 亚洲精品欧美激情专区| 日韩有码中文字幕av| 亚洲人成伊人久久成| 亚洲精品在线观看第一页| 欧美日韩国产成人激情| 国产剧免费看视频网站成人| 亚洲av不卡一区二区在线观看| 很色很污无遮挡的网色污| 每日更新中文字幕粉嫩av| av剧情免费在线观看| 水蜜桃在线观看一区二区国产| 亚洲日本中文字幕免费观看| 综合激情丁香久久狠狠| 2020中文字字幕在线不卡| 亚洲国产成人不卡高清麻豆| 日本特黄特色高清免费大片| 极品女神福利视频久久| 亚洲一区精品中文字幕 | 亚洲国产精品一区二区av |