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

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

2、附上配置文件修改:

<ehcache xmlns:<span id="0_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=16&is_app=0&jk=db7ada96fb08988f&k=xsi&k0=xsi&kdi0=0&luki=4&n=10&p=baidu&q=v77y4_cpr&rb=0&rs=1&seller_id=1&sid=8f9808fb96da7adb&ssp2=1&stid=0&t=tpclicked3_hc&td=2102575&tu=u2102575&u=http%3A%2F%2Fwww%2Eylzx8%2Ecn%2Fyingyongfuwuqi%2Fweb%2Dapplication%2Dserver%2F1777144%2Ehtml&urlid=0" target="_blank" mpid="0" style="color: rgb(1, 70, 108); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">xsi</span></a></span>="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" <span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=16&is_app=0&jk=db7ada96fb08988f&k=memory&k0=memory&kdi0=0&luki=1&n=10&p=baidu&q=v77y4_cpr&rb=0&rs=1&seller_id=1&sid=8f9808fb96da7adb&ssp2=1&stid=0&t=tpclicked3_hc&td=2102575&tu=u2102575&u=http%3A%2F%2Fwww%2Eylzx8%2Ecn%2Fyingyongfuwuqi%2Fweb%2Dapplication%2Dserver%2F1777144%2Ehtml&urlid=0" target="_blank" mpid="1" style="color: rgb(1, 70, 108); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">memory</span></a></span>StoreEvictionPolicy="LRU">
		<cacheEventListenerFactory
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
		<!-- 比一般配置多了这个 -->
		<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
	</cache>

</ehcache>

3、系统初始化时添加:

System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");或者

 System.setProperty("net.sf.ehcache.enableShutdownHook","true");

4.web.xml

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


另外,持久化到硬盘的对象都需要是可序列化的,用以下方法处理:
a)如果类是你自己的,把他设置成可序列化
b)如果类中的某些属性是是第三方jar包的类,可以将它的字段设置成transient(不需序列化)
c)如果类中的某些属性是是第三方jar包但你一定要将所有属性都序列化,可以考虑将这些属性转化成json等

ehcache版本:ehcache-core-2.5.7.jar


例子:

public class CacheOperator
{
  /**
   * 默认cache名称,各个应用自行修改,建议格式:应用名_dist_cache
   */
  public static final String DIST_CACHE = "submitProcessInst";// 分布式cache名字

  private static Cache defaultCache = null;
 
  static
  {
    System.setProperty("net.sf.ehcache.enableShutdownHook","true");
    //System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");
    /**
     * 初始化cache实例
     */
    defaultCache = CacheManager.create().getCache(DIST_CACHE);
  }

  /**
   * 向cache写入数据
   * 
   * @param key
   *          实现序列化接口的key
   * @param value
   *          实现序列化接口的value
   */
  public static void put(Serializable key, Serializable value)
  {
    Element e = new Element(key, value);
    defaultCache.put(e);
  }

  /**
   * 从cache获取数据
   * 
   * @param key
   * @return
   */
  public static Object get(Serializable key)
  {
    return get((Object) key);
  }

  /**
   * 从cache获取数据
   * 
   * @param key
   * @return
   */
  public static Object get(Object key)
  {
    if (defaultCache.isKeyInCache(key))
    {
      Element e = defaultCache.get(key);
      if (e != null)
      {
        return e.getObjectValue();
      }
    }
    return null;
  }

  /**
   * 获取cache对象,直接使用
   * 
   * @return
   */
  public static Cache getDefaultCache()
  {
    return defaultCache;
  }

  public static void remove(Serializable key)
  {
    defaultCache.remove(key);
  }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
  {
    
     for(int i=0;i<20;i++){
    CacheOperator.put("wang"+i, 45645);
    CacheOperator.put("jian"+i, 44);
    CacheOperator.getDefaultCache().flush();

  }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值