ehcache 异常net.sf.ehcache.CacheException: When configured copyOnRead or copyOnWrite, a Store will onl

来源:互联网 发布:photo picture软件 编辑:程序博客网 时间:2024/05/20 00:11

之前遇到“缓存的对象变更后,自动更新到缓存中”的问题,参照http://blog.csdn.net/haiyang4988/article/details/53201618

然后就配置了copyOnWrite=“true”、copyOnRead=“true” 结果又出现了异常(缓存对象的时候)

net.sf.ehcache.CacheException: When configured copyOnRead or copyOnWrite, a Store will only accept Serializable values

最后是这样解决的,在ehcache.xml中配置

    <cache name="conCache"  
           maxElementsOnDisk="20000"  
           maxElementsInMemory="5000"  
           eternal="true"  
           overflowToDisk="false"  
           diskPersistent="false"
           copyOnRead="true"
           copyOnWrite="true"
           memoryStoreEvictionPolicy="LRU">

           <copyStrategy class="com.xxx.MyCopyStrategy" />
           </cache>

然后MyCopyStrategy.java中代码如下

package com.xxxx.ehCache;import java.io.Serializable;import net.sf.ehcache.Element;import net.sf.ehcache.store.compound.ReadWriteCopyStrategy;/**  * 序列化要缓存的对象,否则设置  copyOnRead="true" copyOnWrite="true" 缓存对象的时候可能报序列化错误  * @author CUIJIAJUN * * @date 2016年11月24日 下午2:52:03 * */public class MyCopyStrategy implements ReadWriteCopyStrategy<Element> {@Overridepublic Element copyForWrite(Element value) {if(value != null){//System.out.println("value==="+value);        Object temp=(Serializable)value.getObjectValue();return new Element(value.getObjectKey(),temp);}return value;}@Overridepublic Element copyForRead(Element storedValue) {if(storedValue != null){//System.out.println("storedValue==="+storedValue);        Object temp=(Serializable)storedValue.getObjectValue();return new Element(storedValue.getObjectKey(),temp);}return storedValue;}}

0 0