详解Spring MVC 集成EHCache缓存

2025-05-29 0 68

废话少说,直接上代码:

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缓存

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解Spring MVC 集成EHCache缓存 https://www.kuaiidc.com/116834.html

相关文章

发表评论
暂无评论