一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring MVC 集成EHCache緩存

詳解Spring MVC 集成EHCache緩存

2020-10-20 10:18jiangadam Java教程

本篇文章主要介紹了詳解Spring MVC 集成EHCache緩存,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

廢話少說,直接上代碼:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<ehcache dynamicConfig="false" monitoring="off" updateCheck="false"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
 
  <!-- 定義緩存策略
    eternal="false"         // 元素是否永恒,如果是就永不過期(必須設置)
    maxEntriesLocalHeap="1000"   // 堆內存中最大緩存對象數,0沒有限制(必須設置)
    overflowToDisk="false"     // 當緩存達到maxElementsInMemory值是,是否允許溢出到磁盤(必須設置)
    diskPersistent="false"     // 磁盤緩存在VM重新啟動時是否保持(默認為false)
    timeToIdleSeconds="0"      // 導致元素過期的訪問間隔(秒為單位). 當eternal為false時,這個屬性才有效,0表示可以永遠空閑,默認為0
    timeToLiveSeconds="600"     // 元素在緩存里存在的時間(秒為單位). 0 表示永遠存在不過期
    memoryStoreEvictionPolicy="LFU" // 當達到maxElementsInMemory時,如何強制進行驅逐默認使用"最近使用(LRU)"策略,其它還有先入先出FIFO,最少使用LFU,較少使用LRU
  -->
 
  <!--
    1)maxElementsInMemory(正整數):在內存中緩存的最大對象數量
    2)maxElementsOnDisk(正整數):在磁盤上緩存的最大對象數量,默認值為0,表示不限制。
    3)eternal:設定緩存對象保存的永久屬性,默認為 false 。當為 true 時 timeToIdleSeconds、timeToLiveSeconds 失效。
    4)timeToIdleSeconds(單位:秒): 對象空閑時間,指對象在多長時間沒有被訪問就會失效。只對eternal為false的有效。默認值0,表示一直可以訪問。
    5)timeToLiveSeconds(單位:秒): 對象存活時間,指對象從創建到失效所需要的時間。只對eternal為false的有效。默認值0,表示一直可以訪問。
    6)overflowToDisk:如果內存中數據超過內存限制,是否要緩存到磁盤上。
    7)diskPersistent:是否在磁盤上持久化。指重啟jvm后,數據是否有效。默認為false。
  8)diskSpoolBufferSizeMB(單位:MB): DiskStore使用的磁盤大小,默認值30MB。每個cache使用各自的DiskStore。
    9)memoryStoreEvictionPolicy:如果內存中數據超過內存限制,向磁盤緩存時的策略。默認值LRU,可選FIFO、LFU。
    FIFO(first in first out):先進先出
    LFU(Less Frequently Used):最少被使用,緩存的元素有一個hit屬性,hit值最小的將會被清除緩存。
    LRU(Least Recently Used)默認策略:最近最少使用,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清除緩存。
  10) maxEntriesLocalHeap 堆內存中最大緩存對象數 
  -->
    <diskStore path="java.io.tmpdir"></diskStore>
  <defaultCache
    eternal="false"
    maxEntriesLocalHeap="0"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    maxElementsInMemory="10000"
    overflowToDisk="true"
    diskPersistent="true"
  />
 
  <cache
    name="userCache"
    maxEntriesLocalHeap="10000"
  /> 
  <cache
    name="studentCache"
    maxEntriesLocalHeap="10000"
  />
 
</ehcache>

需要增加的JAR包

詳解Spring MVC 集成EHCache緩存

springmvc.xml 需要在beans增加以下

?
1
2
xmlns:cache="http://www.springframework.org/schema/cache"
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

增加bean

?
1
2
3
4
5
6
7
8
9
<!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/> 
<!-- Spring提供的基于的Ehcache實現的緩存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  <property name="configLocation" value="classpath:config/ehcache.xml"/> 
</bean
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
  <property name="cacheManager" ref="cacheManagerFactory"/> 
</bean>

EHCacheUtils 操作類

?
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
66
67
68
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
 
/**
 * 操作緩存類
 *
 * @author jiangadam
 */
 
public class EhcacheUtils {
 
  private static final String path = "/config/ehcache.xml"; // EHCache 的配置文件地址
 
  private CacheManager manager;
 
  private static EhcacheUtils ehCache;
 
  private EhcacheUtils(String path) {
    manager = CacheManager.create(getClass().getResource(path));
  }
 
  public static EhcacheUtils getInstance() {
    if (ehCache == null) {
      ehCache = new EhcacheUtils(path);
    }
    return ehCache;
  }
 
  /**
   * 緩存一個對象
   *
   * @param cacheName
   *      緩存的名字
   * @param key
   *      緩存的KEY
   * @param value
   *      緩存的值
   */
  public void put(String cacheName, String key, Object value) {
    Cache cache = manager.getCache(cacheName);
    Element element = new Element(key, value);
    cache.put(element);
  }
 
  /**
   * 獲取一個緩存的對象,沒有返回NULL
   *
   * @param cacheName
   * @param key
   * @return
   */
  public Object get(String cacheName, String key) {
    Cache cache = manager.getCache(cacheName);
    Element element = cache.get(key);
    return element == null ? null : element.getObjectValue();
  }
 
  public Cache get(String cacheName) {
    return manager.getCache(cacheName);
  }
 
  public void remove(String cacheName, String key) {
    Cache cache = manager.getCache(cacheName);
    cache.remove(key);
  }
 
}

PUT 寫入緩存

詳解Spring MVC 集成EHCache緩存

GET 獲取緩存的數據

詳解Spring MVC 集成EHCache緩存

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

原文鏈接:http://www.jianshu.com/p/d3de821317b7

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产大秀视频一区二区三区 | 欧美精选欧美极品 | 青草国产福利视频免费观看 | 亚洲偷窥图区色 | 91视频a | 国产一卡2卡3卡四卡国色天香 | 激情亚洲 | 日本一卡二卡3卡四卡无卡网址 | 男人看片网址 | 国产日本久久久久久久久婷婷 | 国产在线视频在线观看 | 亚洲久草视频 | 放荡的女老板bd中文字幕 | 美女脱了内裤打开腿让男人图片 | 奇米影视中文字幕 | 欧美一区二区三区不卡视频 | 欧美精品99久久久久久人 | 亚洲成人综合在线 | 满溢游泳池免费土豪全集下拉版 | 亚洲激情在线 | fuqer老师| www青青草原| 青丝视频免费版在线看 | 男人的j插入女人的p | 男人狂躁女人下半身 | 日韩精品一区二三区中文 | 成人性生交小说免费看 | 狠狠色 | 香蕉eeww99国产精选播放 | 无人区在线观看免费观看 | 国产精品久久久久久爽爽爽 | 91国语自产拍在线观看 | 99精品热线在线观看免费视频 | 国内偷拍第一页 | 国产精品一久久香蕉产线看 | 亚洲日韩精品欧美一区二区 | 男生同性啪视频在线观看 | 大伊人青草狠狠久久 | 成人影院在线观看 | 亚洲女人国产香蕉久久精品 | 欧美性bbbbbxxxxxxx|