激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|

香港云服务器
服務器之家 - 編程語言 - JAVA教程 - Springboot使用cache緩存過程代碼實例

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

2020-06-30 11:43yaominghui JAVA教程

這篇文章主要介紹了Springboot使用cache緩存過程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1.pom.xml

?
1
2
3
4
5
<!-- Ehcache 坐標 -->
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
</dependency>

2.ehcache.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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 表示硬盤中最大緩存對象數無限大
    diskPersistent設置成true表示緩存虛擬機重啟期數據
  -->
  <cache name="usercache"
      maxElementsInMemory="1"
      eternal="true"
      overflowToDisk="true"
      maxElementsOnDisk="0"
      diskPersistent="true">
<!--    <persistence strategy="localTempSwap"/>--> <!--不能和diskPersistent 同時存在-->
  </cache>

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

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

3.application.yml

?
1
2
3
4
spring:
 cache:
  ehcache:
   config: classpath:/ehcache.xml

4.啟動類添加

@EnableCaching

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
  /**
   * 從緩存中獲取數據
   * @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;
  }
 
  /**
   * 數據存入緩存
   * @param data
   * @throws IOException
   */
  public void putCache(String data) throws IOException {
    //若cacheManager被關閉,則重新創建
    if(cacheManager == null || cacheManager.getStatus().intValue() != 1){
      cacheManager = new CacheManager(new ClassPathResource("ehcache.xml").getInputStream());
    }
    Cache cache = cacheManager.getCache("usercache");
    //處理成要緩存的數據
 
    //存入緩存(注意:需要保證存入緩存的數據都是可序列化的)
    cache.put(new Element("name", data));
    /**
     * ehcache和其它緩存類似,需要flush或shutdown后才會持久化到磁盤。
     * 會生成.data 的數據文件和 .index 的索引文件,方便重啟恢復。
     * ehcache恢復數據是根據.index索引文件來進行數據恢復的。
     * 當程序再次啟動的時候,ehcache的一個方法會將.data文件和.index文件的修改時間進行比較,如果不符合直接將.index文件刪除。
     */
    //將所有緩存項從內存刷新到磁盤存儲,并從DiskStore刷新到磁盤。
//    cache.flush();
    //更新.index文件
//    cacheManager.shutdown();
  }
}

6.controller層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
  }
}

結果:

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
538
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 午夜视频在线免费播放 | 日韩视频在线视频 | 性 毛片 | 黄色欧美精品 | 日韩精品一区二区三区中文 | 久久国产乱子伦精品 | 久色免费| av电影网在线观看 | 在线a| 黄色网www| 91精品国产日韩91久久久久久360 | 精品偷拍久久 | 小视频免费在线观看 | 男人午夜小视频 | 欧美aⅴ在线观看 | 国产一区免费在线 | 免费观看视频在线观看 | 日本成人午夜视频 | 日韩中文一区 | 92自拍视频 | 99精品国产成人一区二区 | 中国一级毛片在线视频 | 国产亚洲精品成人a | 亚洲成人久久精品 | 国产亚洲综合一区二区 | 国产九九热视频 | 在线成人av观看 | 久久色网站 | 免费毛片视频播放 | 91精品国产91久久久久久蜜臀 | www亚洲成人 | 涩涩伊人 | 国产精品久久久久久模特 | 91成人免费在线观看 | jizzjizzjizz少妇| 91精品久久久久久久久久久 | 久久精品一区二区三区国产主播 | 草久视频在线观看 | 日本在线视频免费观看 | 中文字幕在线观看免费视频 | 国产成人在线一区二区 |