使用ehcache持久化数据到磁盘 并且在应用服务器重启后不丢失数据
使用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");
另外,持久化到硬盘的对象都需要是可序列化的,用以下方法处理:
a)如果类是你自己的,把他设置成可序列化
b)如果类中的某些属性是是第三方jar包的类,可以将它的字段设置成transient(不需序列化)
c)如果类中的某些属性是是第三方jar包但你一定要将所有属性都序列化,可以考虑将这些属性转化成json等
注意本文转载 未亲自验证
http://www.myexception.cn/web-application-server/1874474.html
最新文章
- bzoj2228[ZJOI2011]礼物(gift)
- web前端基础篇③
- 直接运行可执行文件linux终端一闪而过
- 【转】一个不错的eclipse反编译插件
- C# Window Form播放音乐的4种方式
- hdu 4393 优先队列
- Mysql 升级到 5.6 后插入语句时间字段报错:Incorrect datetime value: &#39;&#39; for column &#39;createtime&#39;
- swift循环
- poj1948
- 用JDBC编程的执行时错误及其解决大全
- 通过blktrace, debugfs分析磁盘IO
- Ubuntu上安装jdk,Jboss
- 使用FastReport的UserDataSet时候,遇到TfrxMemoView内容过多而打印不全的问题
- Hdu5381-The sum of gcd(莫队)
- 剑指offer--二叉树的后序遍历
- .24-浅析webpack源码之事件流compilation(2)
- Windows 产品激活状态、密钥等信息查看
- python中如何删除列表中的所有元素
- linux每日命令(22):find命令参数详解
- 3.2 Bochs
热门文章
- bzoj 3530: [Sdoi2014]数数
- POJ 2923 状压好题
- solution to E: failed to fetch .......
- sql之多表连接
- AndroidManifest.xml file missing!
- BaseHttpListActivity,几行代码搞定Android Http列表请求、加载和缓存
- spring @ModelAttribute 注解
- ASP通过ADODB读取Access数据库
- Python Day04
- “如何稀释scroll事件”的思考(不小心写了个异步do...while)