1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么。。直接可以使用Ehcache
Cache 存储方式 :内存或磁盘
2. 单独使用 EHCache
使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
A:使用默认配置文件创建
- CacheManager manager = CacheManager.create();
CacheManager manager = CacheManager.create();
B:使用指定配置文件创建
- CacheManager manager = CacheManager.create("src/config/ehcache.xml");
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
C:从classpath中找寻配置文件并创建
- URL url = getClass().getResource("/anothername.xml");
- CacheManager manager = CacheManager.create(url);
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
D:通过输入流创建
- InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
- try {
- manager = CacheManager.create(fis);
- } finally {
- fis.close();
- }
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}
卸载CacheManager ,关闭Cache
- manager.shutdown();
manager.shutdown();
使用Caches
取得配置文件中预先 定义的sampleCache1设置,通过CacheManager生成一个Cache
- Cache cache = manager.getCache("sampleCache1");
Cache cache = manager.getCache("sampleCache1");
设置一个名为test 的新cache,test属性为默认
- CacheManager manager = CacheManager.create();
- manager.addCache("test");
CacheManager manager = CacheManager.create();
manager.addCache("test");
设置一个名为test 的新cache,并定义其属性
- CacheManager manager = CacheManager.create();
- Cache cache = new Cache("test", 1, true, false, 5, 2);
- manager.addCache(cache);
CacheManager manager = CacheManager.create();
Cache cache = new Cache("test", 1, true, false, 5, 2);
manager.addCache(cache);
往cache中加入元素
- Element element = new Element("key1", "value1");
- cache.put(new Element(element);
Element element = new Element("key1", "value1");
cache.put(new Element(element);
从cache中取得元素
- Element element = cache.get("key1");
Element element = cache.get("key1");
所以大概步骤为:
第一步:生成CacheManager对象
第二步:生成Cache对象
第三步:向Cache对象里添加由key,value组成的键值对的Element元素
具体一个Test.java程序:
- package test;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
- /**
- * 第一步:生成CacheManager对象
- * 第二步:生成Cache对象
- * 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
- * @author mahaibo
- *
- */
- public class Test {
- public static void main(String[] args) {
- //指定ehcache.xml的位置
- String fileName="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
- CacheManager manager = new CacheManager(fileName);
- //取出所有的cacheName
- String names[] = manager.getCacheNames();
- for(int i=0;i<names.length;i++){
- System.out.println(names[i]);
- }
- //根据cacheName生成一个Cache对象
- //第一种方式:
- Cache cache=manager.getCache(names[0]);
- //第二种方式,ehcache里必须有defaultCache存在,"test"可以换成任何值
- // Cache cache = new Cache("test", 1, true, false, 5, 2);
- // manager.addCache(cache);
- //向Cache对象里添加Element元素,Element元素有key,value键值对组成
- cache.put(new Element("key1","values1"));
- Element element = cache.get("key1");
- System.out.println(element.getValue());
- Object obj = element.getObjectValue();
- System.out.println((String)obj);
- manager.shutdown();
- }
- }
package test;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* 第一步:生成CacheManager对象
* 第二步:生成Cache对象
* 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
* @author mahaibo
*
*/
public class Test {
public static void main(String[] args) {
//指定ehcache.xml的位置
String fileName="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
CacheManager manager = new CacheManager(fileName);
//取出所有的cacheName
String names[] = manager.getCacheNames();
for(int i=0;i<names.length;i++){
System.out.println(names[i]);
}
//根据cacheName生成一个Cache对象
//第一种方式:
Cache cache=manager.getCache(names[0]);
//第二种方式,ehcache里必须有defaultCache存在,"test"可以换成任何值
// Cache cache = new Cache("test", 1, true, false, 5, 2);
// manager.addCache(cache);
//向Cache对象里添加Element元素,Element元素有key,value键值对组成
cache.put(new Element("key1","values1"));
Element element = cache.get("key1");
System.out.println(element.getValue());
Object obj = element.getObjectValue();
System.out.println((String)obj);
manager.shutdown();
}
}
3. 在 Hibernate 中运用EHCache
hibernate.cfg.xml中需设置如下:
2.1版本加入
- <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
2.1以下版本加入
- <property name="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCache</property>
<property name="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCache</property>
在 Hibernate 映射文件的每个需要Cache的Domain中
- <hibernate-mapping>
- <class
- name="com.somecompany.someproject.domain.Country"
- table="ut_Countries"
- dynamic-update="false"
- dynamic-insert="false"
- >
- ...
- </hibernate-mapping>
<hibernate-mapping>
<class
name="com.somecompany.someproject.domain.Country"
table="ut_Countries"
dynamic-update="false"
dynamic-insert="false"
>
...
</hibernate-mapping>
加入类似如下格式信息:
- <cache usage="read-write|nonstrict-read-write|read-only" />
<cache usage="read-write|nonstrict-read-write|read-only" />
比如:
- <cache usage="read-write" />
<cache usage="read-write" />
具体如下:
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.springside.bookstore.plugins.security.domain">
- <class name="User" table="SS_USERS" dynamic-insert="true" dynamic-update="true">
- <cache usage="nonstrict-read-write"/>
- <id name="id" column="ID">
- <generator class="native"/>
- </id>
- <property name="loginid" column="LOGINID" not-null="true"/>
- <property name="passwd" column="PASSWD" not-null="true"/>
- <property name="name" column="NAME" not-null="true"/>
- <property name="email" column="EMAIL"/>
- <property name="region" column="REGION"/>
- <property name="status" column="STATUS"/>
- <property name="descn" column="DESCN"/>
- <set name="roles" table="SS_USER_ROLE" lazy="true" inverse="false" cascade="save-update" batch-size="5">
- <key>
- <column name="USER_ID" not-null="true"/>
- </key>
- <many-to-many class="Role" column="ROLE_ID"/>
- </set>
- </class>
- </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.springside.bookstore.plugins.security.domain">
<class name="User" table="SS_USERS" dynamic-insert="true" dynamic-update="true">
<cache usage="nonstrict-read-write"/>
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="loginid" column="LOGINID" not-null="true"/>
<property name="passwd" column="PASSWD" not-null="true"/>
<property name="name" column="NAME" not-null="true"/>
<property name="email" column="EMAIL"/>
<property name="region" column="REGION"/>
<property name="status" column="STATUS"/>
<property name="descn" column="DESCN"/>
<set name="roles" table="SS_USER_ROLE" lazy="true" inverse="false" cascade="save-update" batch-size="5">
<key>
<column name="USER_ID" not-null="true"/>
</key>
<many-to-many class="Role" column="ROLE_ID"/>
</set>
</class>
</hibernate-mapping>
然后在ehcache.xml中加入
- <ehcache>
- <cache name="com.somecompany.someproject.domain.Country"
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- overflowToDisk="false"
- />
- </ehcache>