欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot使用cache緩存過程代碼實(shí)例

 更新時(shí)間:2020年06月30日 08:32:48   投稿:yaominghui  
這篇文章主要介紹了Springboot使用cache緩存過程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.pom.xml

<!-- Ehcache 坐標(biāo) -->
<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的默認(rèn)緩存策略 -->
  <defaultCache
      maxElementsInMemory="10000"
      eternal="false"
      timeToIdleSeconds="120"
      timeToLiveSeconds="120"
      maxElementsOnDisk="10000000"
      diskExpiryThreadIntervalSeconds="120"
      memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>
  <!--
    maxElementsInMemory設(shè)置成1,overflowToDisk設(shè)置成true,只要有一個(gè)緩存元素,就直接存到硬盤上去
    eternal設(shè)置成true,代表對(duì)象永久有效
    maxElementsOnDisk設(shè)置成0 表示硬盤中最大緩存對(duì)象數(shù)無限大
    diskPersistent設(shè)置成true表示緩存虛擬機(jī)重啟期數(shù)據(jù)
  -->
  <cache name="usercache"
      maxElementsInMemory="1"
      eternal="true"
      overflowToDisk="true"
      maxElementsOnDisk="0"
      diskPersistent="true">
<!--    <persistence strategy="localTempSwap"/>--> <!--不能和diskPersistent 同時(shí)存在-->
  </cache>

diskStore是物理文件的存儲(chǔ)路徑,

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

3.application.yml

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

4.啟動(dòng)類添加

@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
@SpringBootApplication
public 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
 */
@Component
public 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被關(guān)閉,則重新創(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后才會(huì)持久化到磁盤。
     * 會(huì)生成.data 的數(shù)據(jù)文件和 .index 的索引文件,方便重啟恢復(fù)。
     * ehcache恢復(fù)數(shù)據(jù)是根據(jù).index索引文件來進(jìn)行數(shù)據(jù)恢復(fù)的。
     * 當(dāng)程序再次啟動(dòng)的時(shí)候,ehcache的一個(gè)方法會(huì)將.data文件和.index文件的修改時(shí)間進(jìn)行比較,如果不符合直接將.index文件刪除。
     */
    //將所有緩存項(xiàng)從內(nèi)存刷新到磁盤存儲(chǔ),并從DiskStore刷新到磁盤。
//    cache.flush();
    //更新.index文件
//    cacheManager.shutdown();
  }
}

6.controller層

import java.io.IOException;
 
@RestController
public 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;
  }
}

結(jié)果:

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

相關(guān)文章

  • Java this super代碼實(shí)例及使用方法總結(jié)

    Java this super代碼實(shí)例及使用方法總結(jié)

    這篇文章主要介紹了Java this super代碼實(shí)例及使用方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java輸出鏤空金字塔實(shí)現(xiàn)案例

    java輸出鏤空金字塔實(shí)現(xiàn)案例

    小編最近接到領(lǐng)導(dǎo)安排,要求根據(jù)用戶輸入,打印出相應(yīng)層數(shù)的鏤空金字塔效果,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09
  • Mybatis-Plus支持GBase8s分頁(yè)查詢的實(shí)現(xiàn)示例

    Mybatis-Plus支持GBase8s分頁(yè)查詢的實(shí)現(xiàn)示例

    本文主要介紹了使?Mybatis-Plus?支持?GBase8s?的分頁(yè)查詢,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Guava自動(dòng)加載緩存LoadingCache使用實(shí)戰(zhàn)詳解

    Guava自動(dòng)加載緩存LoadingCache使用實(shí)戰(zhàn)詳解

    這篇文章主要為大家介紹了Guava自動(dòng)加載緩存LoadingCache使用實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java實(shí)現(xiàn)添加文字水印和圖片水印功能

    Java實(shí)現(xiàn)添加文字水印和圖片水印功能

    為圖片添加水印是一種常用的圖片處理技術(shù),本文主要介紹了Java實(shí)現(xiàn)添加文字水印和圖片水印功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法

    springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法

    這篇文章主要介紹了springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot中Bean生命周期自定義初始化和銷毀方法詳解

    SpringBoot中Bean生命周期自定義初始化和銷毀方法詳解

    這篇文章給大家詳細(xì)介紹了SpringBoot中Bean生命周期自定義初始化和銷毀方法,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • springboot項(xiàng)目不加端口號(hào)也可以訪問項(xiàng)目的方法步驟分析

    springboot項(xiàng)目不加端口號(hào)也可以訪問項(xiàng)目的方法步驟分析

    這篇文章主要介紹了springboot項(xiàng)目不加端口號(hào)也可以訪問項(xiàng)目的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 如何在SpringBoot項(xiàng)目里進(jìn)行統(tǒng)一異常處理

    如何在SpringBoot項(xiàng)目里進(jìn)行統(tǒng)一異常處理

    這篇文章主要介紹了如何在SpringBoot項(xiàng)目里進(jìn)行統(tǒng)一異常處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。需要的小伙伴可以參考一下
    2022-07-07
  • 詳解Jmeter中的BeanShell腳本

    詳解Jmeter中的BeanShell腳本

    BeanShell是一種完全符合Java語(yǔ)法規(guī)范的腳本語(yǔ)言,并且又擁有自己的一些語(yǔ)法和方法,所以它和java是可以無縫銜接的,學(xué)了Java的一些基本語(yǔ)法后,就可以來在Jmeter中寫寫B(tài)eanShell腳本了
    2021-12-12

最新評(píng)論