使用ehcache时如何持久化数据到磁盘,并且在应用服务器重启后不丢失数据

1、如何持久化到磁盘
使用cache.flush(),每次写入到cache后调用cache.flush() ,这样ehcache 会将索引(xxx.index)回写到磁盘。这样就不用担心程序是否非正常退出导致缓存丢失了。

2、附上配置文件修改:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="ehcache.xsd" name="ehcache">
	<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=manual"/>

	<diskStore path="d:/ehcache"/>
	<cache name="submitProcessInst" maxElementsInMemory="1" eternal="true"
		overflowToDisk="true" diskSpoolBufferSizeMB="10" maxElementsOnDisk="1000000"
		diskPersistent="true" memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
		<!-- 比一般配置多了这个 -->
		<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
	</cache>

</ehcache>
注意:当不需保存数据在内存中时,将maxElementsInMemory="1",而不是0,设置为0时,可以看到ehcache有warning:
2015-03-10 10:44:28,469 WARN  net.sf.ehcache.config.CacheConfiguration.warnMaxEntriesLocalHeap(CacheConfiguration.java:1601) - Cache: submitProcessInst has a maxElementsInMemory of 0. This might lead to performance degradation or OutOfMemoryError at Terracotta client.From Ehcache 2.0 onwards this has been changed to mean a store with no capacity limit. Set it to 1 if you want no elements cached in memory

3、系统初始化时添加:
System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");

4、在web.xml添加listener,让应用程序关闭时通知ehcache

	<listener>
		<listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
	</listener>

5、修改ehcache源码,让它不会轻易删除index文件,因为index文件若删除了,cache数据就会清零。

修改ehcache源码中的 net.sf.ehcache.store.disk.DiskStorageFactory类中的 DiskStorageFactory 方法,注释最后else if 的代码



另外,持久化到硬盘的对象都需要是可序列化的,用以下方法处理:

a)如果类是你自己的,把他设置成可序列化
b)如果类中的某些属性是是第三方jar包的类,可以将它的字段设置成transient(不需序列化)
c)如果类中的某些属性是是第三方jar包但你一定要将所有属性都序列化,可以考虑将这些属性转化成json等

ehcache版本:ehcache-core-2.5.2.jar

集群中使用ehcache
http://www.ibm.com/developerworks/cn/java/j-lo-ehcache/
http://www.cnblogs.com/yangy608/archive/2011/10/07/2200669.html

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值