最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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
@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被關閉,則重新創(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文件刪除。
     */
    //將所有緩存項從內(nèi)存刷新到磁盤存儲,并從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;
  }
}

結果:

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

相關文章

  • 簡單介紹Java編程中的線程池

    簡單介紹Java編程中的線程池

    這篇文章主要介紹了Java編程中的線程池,進程和線程的并發(fā)是Java編程中的重要環(huán)節(jié),需要的朋友可以參考下
    2015-09-09
  • 詳解 Java中日期數(shù)據(jù)類型的處理之格式轉換的實例

    詳解 Java中日期數(shù)據(jù)類型的處理之格式轉換的實例

    這篇文章主要介紹了詳解 Java中日期數(shù)據(jù)類型的處理之格式轉換的實例的相關資料,日期以及時間格式處理,在Java中時間格式一般會涉及到的數(shù)據(jù)類型包括Calendar類和Date類,需要的朋友可以參考下
    2017-08-08
  • 詳解Java 微服務架構

    詳解Java 微服務架構

    這篇文章主要介紹了Java 微服務架構的相關資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Java操作itextpdf實現(xiàn)PDF添加文字,圖片和簽名

    Java操作itextpdf實現(xiàn)PDF添加文字,圖片和簽名

    這篇文章主要為大家詳細介紹了Java如何操作itextpdf實現(xiàn)PDF添加文字,圖片和簽名等功能,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2025-01-01
  • IDEA的Mybatis Log Plugin插件配置和使用詳解

    IDEA的Mybatis Log Plugin插件配置和使用詳解

    這篇文章主要介紹了IDEA的Mybatis Log Plugin插件配置和使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java Math類的三個方法ceil,floor,round用法

    Java Math類的三個方法ceil,floor,round用法

    這篇文章主要介紹了Java Math類的三個方法ceil,floor,round用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring security登錄成功后跳轉回登錄前的頁面

    spring security登錄成功后跳轉回登錄前的頁面

    這篇文章主要介紹了spring security登錄成功后跳轉回登錄前的頁面,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解Spring中BeanUtils工具類的使用

    詳解Spring中BeanUtils工具類的使用

    這篇文章主要通過一些示例為大家詳細介紹了Spring中BeanUtils工具類的使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-06-06
  • 基于Java Callable接口實現(xiàn)線程代碼實例

    基于Java Callable接口實現(xiàn)線程代碼實例

    這篇文章主要介紹了基于Java Callable接口實現(xiàn)線程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Java實現(xiàn)調(diào)用外部程序的示例代碼

    Java實現(xiàn)調(diào)用外部程序的示例代碼

    本文主要介紹了Java實現(xiàn)調(diào)用外部程序的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05

最新評論

兴城市| 芦溪县| 富民县| 道真| 丽江市| 获嘉县| 合山市| 阜平县| 汝南县| 桃江县| 凤凰县| 延津县| 马尔康县| 中超| 敦化市| 东丰县| 潞城市| 吉木萨尔县| 漳浦县| 双城市| 镇宁| 乌恰县| 邵阳市| 资溪县| 交口县| 疏勒县| 偃师市| 宁城县| 田东县| 厦门市| 长武县| 泰安市| 大兴区| 黑水县| 永年县| 樟树市| 静宁县| 隆回县| 弥渡县| 尚志市| 莲花县|