How to persist data to disk when using Ehcache, and do not lose data after the application server restarts

Source: Internet
Author: User

1. How to persist to disk
With Cache.flush (), Cache.flush () is called after each write to the cache, so that Ehcache writes the index (XXX.INDEX) back to disk. This allows the cache to be lost without worrying about whether or not the program exits abnormally.

2, attach the configuration file modification:

<ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= "ehcache.xsd" Name= "Ehcache" ><cachemanagerpeerproviderfactoryclass= " Net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory "properties=" Peerdiscovery=manual "/>< Diskstore path= "D:/ehcache"/><cache name= "Submitprocessinst" maxelementsinmemory= "1" eternal= "true" Overflowtodisk= "true" diskspoolbuffersizemb= "ten" maxelementsondisk= "1000000" diskpersistent= "true" Memorystoreevictionpolicy= "LRU" ><cacheeventlistenerfactoryclass= " Net.sf.ehcache.distribution.RMICacheReplicatorFactory "/><!--more than the general configuration of this-->< Bootstrapcacheloaderfactory class= "Net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/></cache ></ehcache>
Note: When you do not need to save the data in memory, maxelementsinmemory= "1" instead of 0, set to 0 o'clock, you can see that Ehcache has 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 leads to performance degradation or OutOfMemoryError at Terracotta client. From Ehcache 2.0 onwards this have been changed to mean a store with no capacity limit. Set it to 1 if you want no elements cached in memory

3. When the system is initialized, add:
System.setproperty (Net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY, "true");

In addition, objects that are persisted to the hard disk need to be serializable and are handled in the following ways:
A) If the class is your own, set it to serializable
b) If some of the properties in the class are third-party jar package classes, you can set its field to transient (no serialization required)
c) If some of the properties in the class are third-party jar packages but you have to serialize all of the properties, consider converting them to JSON, etc.

Ehcache version: Ehcache-core-2.5.2.jar

Using Ehcache in a cluster
http://www.ibm.com/developerworks/cn/java/j-lo-ehcache/
Http://www.cnblogs.com/yangy608/archive/2011/10/07/2200669.html

How to persist data to disk when using Ehcache, and do not lose data after the application server restarts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.