Ehcache持久化小插曲
昨天在尝试测试ehcache的持久化 过程中,遇到了写问题。
今天突然想明白,顺便过来记录一下
(ehcache持久化的 对象 必须是可序列化的)
持久化到硬盘的对象都需要是可序列化的,用以下方法处理:
a)如果类是你自己的,把他设置成可序列化
b)如果类中的某些属性是是第三方jar包的类,可以将它的字段设置成transient(不需序列化)
c)如果类中的某些属性是是第三方jar包但你一定要将所有属性都序列化,可以考虑将这些属性转化成json等
现将代码黏贴至此:
持久化对象:
public class Person implements Serializable {
private static final long serialVersionUID = -7441447303797910095L;
private Integer id;
private String name;
private int age;
private String gender;
public Person() {
super();
}
public Person(Integer id, String name, int age, String gender) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="C:/ehcache"/>
<defaultCache
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="test11" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
<!--
FIFO first in first out
LRU 淘汰最长时间未被使用
LFU 淘汰一定时期内被访问次数最少
-->
<cache name="sample" maxElementsInMemory="100"
maxElementsOnDisk="0" eternal="true" overflowToDisk="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" diskPersistent="true" />
<cache
name="a"
maxElementsInMemory="1"
eternal="true"
overflowToDisk="true"
maxElementsOnDisk="0"
diskPersistent="true">
</cache>
</ehcache>
测试代码:
package com.lee.ehcache.test;
import org.junit.Test;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhcacheTest {
/**
* 五、Disk persistence
* 获取从disk上的数据
*/
@Test
public void testDiskPersist2() {
String cacheName = "sample";
CacheManager cm = new CacheManager("src/main/resources/ehcache_1.xml");
Cache cache = cm.getCache(cacheName);
long memeryStoreSize = cache.getMemoryStoreSize();
long diskStoreSize = cache.getDiskStoreSize();
System.out.println(memeryStoreSize);
System.out.println(diskStoreSize);
System.out.println("********************");
for(int i=1;i<50;i++) {
Element ele = cache.get("key"+i);
System.out.println(ele);
}
cm.shutdown();
}
/**
* 四、Disk persistence
*/
@Test
public void testDiskPersist() {
String cacheName = "sample";
CacheManager cm = new CacheManager("src/main/resources/ehcache_1.xml");
Cache cache = cm.getCache(cacheName);
System.out.println(cache);
for (int i = 0; i < 5000; i++) {
Element ele = new Element("key"+i, new Person(i, "lee"+i, 20+i, "male"));
cache.put(ele);
}
cache.flush();
System.out.println(cache.getMemoryStoreSize());
System.out.println(cache.getDiskStoreSize());
cm.shutdown();
}
/**
* 三、crud
*/
@Test
public void testCRUD() {
String cacheName = "sample";
CacheManager cm = new CacheManager("src/main/resources/ehcache_1.xml");
Cache cache = cm.getCache(cacheName);
//put an element into cache
Element ele1 = new Element("k1", new Person(1, "lee", 20, "male"));//element 存储 缓存对象和 存在时间 、设置时间、 更新时间等信息
cache.put(ele1);
Element ele2 = new Element("k2", new Person(1, "taty", 2, "female"));
cache.put(ele2);
Element res1 = cache.get("k1");
System.out.println(res1);
if(res1!=null) {
String pk1 = (String) res1.getObjectKey();
Person pv1 = (Person) res1.getObjectValue();
System.out.println("pre:"+pk1+"----"+pv1);
}
System.out.println("***********************");
//移除 缓存
cache.remove("k1");
Element afres1 = cache.get("k1");
System.out.println(afres1);
if(afres1!=null) {
String afpk1 = (String) afres1.getObjectKey();
Person afpv1 = (Person) afres1.getObjectValue();
System.out.println("after:"+afpk1+"----"+afpv1);
}
}
/**
* 二、cache
*/
@Test
public void testCache() {
String cacheName = "sample";
CacheManager cm = new CacheManager("src/main/resources/ehcache_1.xml");
Cache cache = cm.getCache(cacheName);
System.out.println(cache);
}
/**
* 一、cacheManager
*/
@Test
public void testCacheManeger() {
//1、
// CacheManager cm = CacheManager.getInstance();
//2、
// CacheManager cm = new CacheManager("src/main/resources/ehcache_1.xml");
//
// String[] cacheNames = cm.getCacheNames();
// System.out.println(cacheNames.length);
// for(String name : cacheNames) {
// System.out.println(name);
// }
//3、
CacheManager cm = CacheManager.create();
cm.addCache("lee");
Cache cache = cm.getCache("lee");
System.out.println(cache);
//移除cache
cm.removeCache("lee");
System.out.println(cache.getStatus());
//关闭cacheManager
cm.shutdown();
}
}
运行第四个 和第五个方法: