ehcache demo

网上找了一下ehcache的例子,感觉都不好,就自己写了一个简单的demo

在eclipse下建个java工程。

新建User.java类

package net.ehcache.demo;

import java.io.Serializable;

//放在cache里面的对象必须继承Serializable对象,这也是hibernate实体对象继承Serializable的原因。

public class User implements Serializable{
private static final long serialVersionUID = -6227376640818226890L;
private String name;
private String code;

public User(String name, String code) {
super();
this.name = name;
this.code = code;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}

新建HelloWorld.java类

package net.ehcache.demo;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class HelloWorld {

public static void main(String[] args) {
//创建对象有很多种方式,比较常用的是根据配置文件创建,CacheManager manager = new CacheManager("src/config/other.xml");
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 50000, false, false, 8, 2);
singletonManager.addCache(memoryOnlyCache);
//得到cache对象
Cache tesCache = singletonManager.getCache("testCache");

tesCache.put(new Element("key1","value1"));
tesCache.put(new Element("key2","value2"));

Element eleKey1 = tesCache.get("key1");

Serializable value = eleKey1.getValue();
System.out.println("key1 Object value " + value);
Object ovalue = eleKey1.getObjectValue();
System.out.println("key1 Object value " + ovalue);

//Element对象可以是任何继承Serializable接口的对象,也就是说cache可以存储任何Serializable对象。
User testUser1 = new User("张三","zhangsan");
tesCache.put(new Element("zhangsan",testUser1));
Element eleUser1 = tesCache.get("zhangsan");

value = eleUser1.getValue();
System.out.println("eleUser1 Object value " + value);
ovalue = eleUser1.getObjectValue();
System.out.println("eleUser1 Object value " + ovalue);
String userName = ((User)ovalue).getName();
System.out.println("eleUser1 getName is " + userName);

singletonManager.removeCache("testCache");
CacheManager.getInstance().shutdown();
}
}

到此demo完成。

本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值