亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot如何使用EhCache演示

 更新時(shí)間:2019年10月24日 10:16:42   投稿:yaominghui  
這篇文章主要介紹了Spring Boot如何使用EhCache演示,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、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)依賴。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--ehcache依賴-->
<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">

  <!--
    磁盤存儲(chǔ):將緩存中暫時(shí)不使用的對(duì)象,轉(zhuǎn)移到硬盤,類似于Windows系統(tǒng)的虛擬內(nèi)存
    path:指定在硬盤上存儲(chǔ)對(duì)象的路徑
    path可以配置的目錄有:
    user.home(用戶的家目錄)
    user.dir(用戶當(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)的緩存配置信息,如果不加特殊說明,則所有對(duì)象按照此配置項(xiàng)處理
    maxElementsInMemory:設(shè)置了緩存的上限,最多存儲(chǔ)多少個(gè)記錄對(duì)象
    eternal:代表對(duì)象是否永不過期 (指定true則下面兩項(xiàng)配置需為0無限期)
    timeToIdleSeconds:最大的發(fā)呆時(shí)間 /秒
    timeToLiveSeconds:最大的存活時(shí)間 /秒
    overflowToDisk:是否允許對(duì)象被寫入到磁盤
    說明:下列配置自緩存建立起600秒(10分鐘)有效 。
    在有效的600秒(10分鐘)內(nèi),如果連續(xù)120秒(2分鐘)未訪問緩存,則緩存失效。
    就算有訪問,也只會(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配置文件中,指明緩存類型并聲明Ehcache配置文件的位置。

server:
 port: 10900

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

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

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

@EnableCaching
@SpringBootApplication
public 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)用類,方便調(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ù)處理類,添加緩存,更新緩存和刪除。

@Slf4j
@Service
public 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 void del(int id){
    log.info("刪除id{}數(shù)據(jù)", id);
    dataMap.remove(id);
  }  
}

源碼地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 23種設(shè)計(jì)模式(16)java訪問者模式

    23種設(shè)計(jì)模式(16)java訪問者模式

    這篇文章主要為大家詳細(xì)介紹了23種設(shè)計(jì)模式之java訪問者模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • MyBatis-Plus+達(dá)夢(mèng)數(shù)據(jù)庫實(shí)現(xiàn)高效數(shù)據(jù)持久化的示例

    MyBatis-Plus+達(dá)夢(mèng)數(shù)據(jù)庫實(shí)現(xiàn)高效數(shù)據(jù)持久化的示例

    這篇文章主要介紹了MyBatis-Plus和達(dá)夢(mèng)數(shù)據(jù)庫實(shí)現(xiàn)高效數(shù)據(jù)持久化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Mybatis多線程下如何使用Example詳解

    Mybatis多線程下如何使用Example詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis多線程下如何使用Example的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • SpringBoot整合WebService服務(wù)的實(shí)現(xiàn)代碼

    SpringBoot整合WebService服務(wù)的實(shí)現(xiàn)代碼

    WebService是一個(gè)SOA(面向服務(wù)的編程)的架構(gòu),它是不依賴于語言,不依賴于平臺(tái),可以實(shí)現(xiàn)不同的語言間的相互調(diào)用,通過Internet進(jìn)行基于Http協(xié)議的網(wǎng)絡(luò)應(yīng)用間的交互,這篇文章主要介紹了SpringBoot整合WebService服務(wù)的實(shí)例代碼,需要的朋友可以參考下
    2022-02-02
  • Java中獲取List中最后一個(gè)元素的三種方法

    Java中獲取List中最后一個(gè)元素的三種方法

    在Java編程中我們經(jīng)常需要獲取一個(gè)List集合中的最后一個(gè)元素,這篇文章主要給大家介紹了關(guān)于Java中獲取List中最后一個(gè)元素的三種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Java中Map與JSON數(shù)據(jù)之間的互相轉(zhuǎn)化

    Java中Map與JSON數(shù)據(jù)之間的互相轉(zhuǎn)化

    我們?cè)陂_發(fā)中難免和JSON打交道,這不小編最近遇到了,需要把一些信息轉(zhuǎn)成JSON字符串,下面這篇文章主要給大家介紹了關(guān)于Java中Map與JSON數(shù)據(jù)之間的互相轉(zhuǎn)化,需要的朋友可以參考下
    2023-04-04
  • 詳解MyBatis自定義Plugin插件

    詳解MyBatis自定義Plugin插件

    這篇文章主要介紹了MyBatis自定義Plugin插件的相關(guān)知識(shí),實(shí)現(xiàn)方法也很簡單,只需實(shí)現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可,需要的朋友可以參考下
    2018-06-06
  • SpringBoot沒有讀取到application.yml問題及解決

    SpringBoot沒有讀取到application.yml問題及解決

    這篇文章主要介紹了SpringBoot沒有讀取到application.yml問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringBoot SpEL語法掃盲與查詢手冊(cè)的實(shí)現(xiàn)

    SpringBoot SpEL語法掃盲與查詢手冊(cè)的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot SpEL語法掃盲與查詢手冊(cè)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 基于java Servlet編碼/異常處理(詳解)

    基于java Servlet編碼/異常處理(詳解)

    下面小編就為大家?guī)硪黄趈ava Servlet編碼/異常處理(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10

最新評(píng)論