EhCache之初试

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

详情参见其官网站:http://ehcache.org/

具体文件下载网站:http://sourceforge.net/projects/ehcache/files/

下面将用实际的Demo分类演示介绍EhCache的初步使用:

  • 单例CacheManager 创建
  • 多个CacheManager 创建
  • Cache的多种创建方式
  • 对Cache的CRUD的操作
  • Cache的统计信息
  • 测试所用两个的ehcache.xml配置文件

[一]、单例CacheManager 创建

1.代码示例:

2.运行结果:

Create a singleton CacheManager using defaults
CacheManager.create() :=net.sf.ehcache.CacheManager@e80842
cacheNames length := 4
=======================================
Create a singleton CacheManager using a configuration file
CacheManager.create(file) :=net.sf.ehcache.CacheManager@1bbf1ca
cacheNames length := 6
CacheManager.getInstance() == singletonManager2 :: true
=======================================
CacheManager.create(url) :=net.sf.ehcache.CacheManager@f9c40
cacheNames length := 6
name := sampleRepicatedCache2
name := sampleReplicatedCache1
name := sampleCache2
name := sampleCache1
name := sampleReplicatedCache3
name := sampleCache3

[二]、多个CacheManager 创建

1.代码示例:

2.运行结果:

new CacheManager() := net.sf.ehcache.CacheManager@17653ae
cacheNames length := 4
name := sampleCache2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := org.hibernate.cache.StandardQueryCache
=======================================
new CacheManager(file) := net.sf.ehcache.CacheManager@1ff0dde
cacheNames length := 6
=======================================
new CacheManager(url) := net.sf.ehcache.CacheManager@db4fa2
cacheNames length := 6
name := sampleRepicatedCache2
name := sampleReplicatedCache1
name := sampleCache2
name := sampleCache1
name := sampleReplicatedCache3
name := sampleCache3

[三]、Cache的多种创建方式

1.代码示例:

2.运行结果:

add cache with defaults:
[ name = myCache1 status = STATUS_ALIVE eternal = false overflowToDisk = true maxElementsInMemory = 100 maxElementsOnDisk = 100 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 120 timeToIdleSeconds = 120 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 overflowToOffHeap = false maxMemoryOffHeap = null ]
add cache with new Cache(arg1,arg2…):
[ name = myMemoryCache status = STATUS_ALIVE eternal = false overflowToDisk = false maxElementsInMemory = 5000 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 5 timeToIdleSeconds = 2 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 overflowToOffHeap = false maxMemoryOffHeap = null ]
Create a Cache specifying its configuration:
[ name = myConifgCahce status = STATUS_ALIVE eternal = false overflowToDisk = true maxElementsInMemory = 100 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LFU timeToLiveSeconds = 60 timeToIdleSeconds = 30 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 overflowToOffHeap = false maxMemoryOffHeap = null ]

[四]、对Cache的CRUD的操作

1.代码示例:

2.运行结果:

manager.getCache :[ name = MichaelInfo status = STATUS_ALIVE eternal = false overflowToDisk = true maxElementsInMemory = 100 maxElementsOnDisk = 100 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 120 timeToIdleSeconds = 120 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 overflowToOffHeap = false maxMemoryOffHeap = null ]
cache put Element
get Element value:= http://www.micmiu.com
get Element objectvalue:= http://www.micmiue.com
update the Element
get Element value:= http://www.micmiu.com
get Element objectvalue:= http://www.micmiu.com
array value:= [Ljava.lang.String;@ec4a87
remove Element ‘array’ successful.

[五]、Cache的统计信息

1.代码示例:

2.运行结果:

cache size := 6
MemoryStoreSize := 6
DiskStoreSize := 0
Caceh getStatistics:
CacheHits := 0
CacheMisses := 0
InMemoryHits := 0
InMemoryMisses := 0
OnDiskHits := 0
OnDiskMisses := 0
MemoryStoreObjectCount := 6
DiskStoreObjectCount := 0
Element HitCount := 1

[六]、测试所用两个的ehcache.xml文件

1.classpath 下的: ehcache.xml (默认加载的配置文件)

2.michael/hibernate/cache/ehcache/ehcache.xml指定的配置文件

本文对EhCache的初步使用示例就讲到这,下一节将讲一讲EhCache和Hibernate的集成应用示例.

原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]

本文链接地址: http://www.micmiu.com/architecture/cache/ehcache-start-demo/

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">