我是靠谱客的博主 失眠冥王星,这篇文章主要介绍EHCache安装配置详解,现在分享给大家,希望可以做个参考。

为什么80%的码农都做不了架构师?>>>   hot3.png

1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么。。直接可以使用Ehcache

Cache 存储方式 :内存或磁盘

2. 单独使用 EHCache

使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
A:使用默认配置文件创建
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. CacheManager manager = CacheManager.create();
复制代码
1
CacheManager manager = CacheManager.create();

B:使用指定配置文件创建
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. CacheManager manager = CacheManager.create("src/config/ehcache.xml");
复制代码
1
CacheManager manager = CacheManager.create("src/config/ehcache.xml");

C:从classpath中找寻配置文件并创建
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. URL url = getClass().getResource("/anothername.xml");
  2. CacheManager manager = CacheManager.create(url);
复制代码
1
2
URL url = getClass().getResource("/anothername.xml"); CacheManager manager = CacheManager.create(url);

D:通过输入流创建
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
  2. try {
  3. manager = CacheManager.create(fis);
  4. } finally {
  5. fis.close();
  6. }
复制代码
1
2
3
4
5
6
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); try { manager = CacheManager.create(fis); } finally { fis.close(); }


卸载CacheManager ,关闭Cache

Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. manager.shutdown();
复制代码
1
manager.shutdown();


使用Caches

取得配置文件中预先 定义的sampleCache1设置,通过CacheManager生成一个Cache

Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. Cache cache = manager.getCache("sampleCache1");
复制代码
1
Cache cache = manager.getCache("sampleCache1");



设置一个名为test 的新cache,test属性为默认

Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. CacheManager manager = CacheManager.create();
  2. manager.addCache("test");
复制代码
1
2
CacheManager manager = CacheManager.create(); manager.addCache("test");


设置一个名为test 的新cache,并定义其属性
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. CacheManager manager = CacheManager.create();
  2. Cache cache = new Cache("test", 1, true, false, 5, 2);
  3. manager.addCache(cache);
复制代码
1
2
3
CacheManager manager = CacheManager.create(); Cache cache = new Cache("test", 1, true, false, 5, 2); manager.addCache(cache);



往cache中加入元素
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. Element element = new Element("key1", "value1");
  2. cache.put(new Element(element);
复制代码
1
2
Element element = new Element("key1", "value1"); cache.put(new Element(element);



从cache中取得元素
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. Element element = cache.get("key1");
复制代码
1
Element element = cache.get("key1");


所以大概步骤为:
第一步:生成CacheManager对象
第二步:生成Cache对象
第三步:向Cache对象里添加由key,value组成的键值对的Element元素


具体一个Test.java程序:
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. package test;
  2. import net.sf.ehcache.Cache;
  3. import net.sf.ehcache.CacheManager;
  4. import net.sf.ehcache.Element;
  5. /**
  6. * 第一步:生成CacheManager对象
  7. * 第二步:生成Cache对象
  8. * 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
  9. * @author mahaibo
  10. *
  11. */
  12. public class Test {
  13. public static void main(String[] args) {
  14. //指定ehcache.xml的位置
  15. String fileName="E:\1008\workspace\ehcachetest\ehcache.xml";
  16. CacheManager manager = new CacheManager(fileName);
  17. //取出所有的cacheName
  18. String names[] = manager.getCacheNames();
  19. for(int i=;i<names.length;i++){
  20. System.out.println(names[i]);
  21. }
  22. //根据cacheName生成一个Cache对象
  23. //第一种方式:
  24. Cache cache=manager.getCache(names[]);
  25. //第二种方式,ehcache里必须有defaultCache存在,"test"可以换成任何值
  26. // Cache cache = new Cache("test", 1, true, false, 5, 2);
  27. // manager.addCache(cache);
  28. //向Cache对象里添加Element元素,Element元素有key,value键值对组成
  29. cache.put(new Element("key1","values1"));
  30. Element element = cache.get("key1");
  31. System.out.println(element.getValue());
  32. Object obj = element.getObjectValue();
  33. System.out.println((String)obj);
  34. manager.shutdown();
  35. }
  36. }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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版本加入
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
复制代码
1
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>

2.1以下版本加入
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <property name="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCache</property>
复制代码
1
<property name="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCache</property>


在 Hibernate 映射文件的每个需要Cache的Domain中
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <hibernate-mapping>
  2. <class
  3. name="com.somecompany.someproject.domain.Country"
  4. table="ut_Countries"
  5. dynamic-update="false"
  6. dynamic-insert="false"
  7. >
  8. ...
  9. </hibernate-mapping>
复制代码
1
2
3
4
5
6
7
8
9
10
<hibernate-mapping> <class name="com.somecompany.someproject.domain.Country" table="ut_Countries" dynamic-update="false" dynamic-insert="false" > ... </hibernate-mapping>

加入类似如下格式信息:
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <cache usage="read-write|nonstrict-read-write|read-only" />
复制代码
1
<cache usage="read-write|nonstrict-read-write|read-only" />

比如:
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <cache usage="read-write" />
复制代码
1
<cache usage="read-write" />


具体如下:

Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="org.springside.bookstore.plugins.security.domain">
  5. <class name="User" table="SS_USERS" dynamic-insert="true" dynamic-update="true">
  6. <cache usage="nonstrict-read-write"/>
  7. <id name="id" column="ID">
  8. <generator class="native"/>
  9. </id>
  10. <property name="loginid" column="LOGINID" not-null="true"/>
  11. <property name="passwd" column="PASSWD" not-null="true"/>
  12. <property name="name" column="NAME" not-null="true"/>
  13. <property name="email" column="EMAIL"/>
  14. <property name="region" column="REGION"/>
  15. <property name="status" column="STATUS"/>
  16. <property name="descn" column="DESCN"/>
  17. <set name="roles" table="SS_USER_ROLE" lazy="true" inverse="false" cascade="save-update" batch-size="5">
  18. <key>
  19. <column name="USER_ID" not-null="true"/>
  20. </key>
  21. <many-to-many class="Role" column="ROLE_ID"/>
  22. </set>
  23. </class>
  24. </hibernate-mapping>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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中加入
Java代码 复制代码 收藏代码29153947_bWnX.gif
  1. <ehcache>
  2. <cache name="com.somecompany.someproject.domain.Country"
  3. maxElementsInMemory="10000"
  4. eternal="false"
  5. timeToIdleSeconds="300"
  6. timeToLiveSeconds="600"
  7. overflowToDisk="false"
  8. />
  9. </ehcache>

转载于:https://my.oschina.net/dyyweb/blog/34342

最后

以上就是失眠冥王星最近收集整理的关于EHCache安装配置详解的全部内容,更多相关EHCache安装配置详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部