写的比较初级,后续将加入对配置文件,内存/磁盘存储,优化,虚拟机宕机,日志等的说明
1. EHCache 的特点,系统要求及安装
是一个纯Java ,过程中(也可以理解成插入式)缓存实现
Hibernate2.1,Spring支持EHcache嵌入,自我感觉Hibernate2.1 + EHCache 很过瘾。。(测试结果比Hibernate+JCS 好多了)
支持多CPU服务器
其版本发布前进行了产品级测试
支持:运行环境jdk1.2到5版本(源代码编译需1.4或1.5 版本)
jdk1.4和1.5 版本,需加入apache 的 commons-logging类库 http://jakarta.apache.org/commons/logging.html
jdk1.2和1.3 版本,需加入commons-collections 2.1 版本 http://jakarta.apache.org/commons/collections.html
和 xerces (xml-apis.jar and xercesImpl.jar), 2.5 版本 http://xml.apache.org/xerces2-j/
如果运行环境为IBM JDK1.4版本上的IBM Websphere 5.1,也需加入 commons-collections 类库(如需要)
单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。
如项目已安装了Hibernate2.1 ,则不需要做什么。。直接可以使用Ehcache
Cache 存储方式 :内存或磁盘
2. 单独使用 EHCache
使用CacheManager 创建并管理Cache
使用默认配置文件创建
CacheManager manager = CacheManager.create();
使用指定配置文件创建
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
从classpathq找寻配置文件并创建
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
通过输入流创建
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}
卸载CacheManager ,关闭Cache
manager.shutdown();
使用Caches
取得配置文件中预先 定义的sampleCache1设置,生成一个Cache
Cache cache = manager.getCache("sampleCache1");
设置一个名为test 的新cache,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);
往cache中加入元素
Element element = new Element("key1", "value1");
cache.put(new Element(element);
从cache中取得元素
Element element = cache.get("key1");
3. 在 Hibernate 中运用EHCache
hibernate.cfg.xml中需设置如下:
2.1版本加入
<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>
在 Hibernate 映射文件的每个需要Cache的Domain中
<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" />
然后在ehcache.xml中加入
<ehcache>
<cache name="com.somecompany.someproject.domain.Country"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
/>
</ehcache>
分享到:
相关推荐
<property name="hibernate.cache.use_second_level_cache">true ``` 同时,还需要在EHcache的配置文件(如`ehcache.xml`)中声明具体的缓存: ```xml maxEntriesLocalHeap="1000" eternal="false" ...
**Ehcache缓存** Ehcache是一种广泛使用的开源Java分布式缓存系统,它为高性能应用程序提供了内存存储和缓存解决方案。在Java世界中,尤其是在持久化框架如Hibernate的使用中,Ehcache扮演了至关重要的角色。由于...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 3. **配置Ehcache**:创建...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory <property name="net.sf.ehcache....
<pbcast.NAKACK2 use_mcast_xmit="false" retransmit_timeout="600,1200,2400,4800" /> ``` 这里定义了TCP协议栈,用于节点间的通信,并设置了成员发现和故障检测机制。 4. **初始化Ehcache**:在应用...
这里,`hibernate.cache.use_second_level_cache`属性开启二级缓存,`hibernate.cache.region.factory_class`指定了Ehcache作为区域工厂,`net.sf.ehcache.configurationResourceName`则是Ehcache配置文件的位置。 ...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory <property name="net.sf.ehcache....
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 3. **配置ehcache.xml**:创建...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory <!-- entity class --> @Entity @...
如果想要查询结果被缓存,需确保`hibernate.cache.use_query_cache`为true,并调用`Query.setCacheable(true)`。 Ehcache允许开发者进行个性化定制,例如更改缓存的存储位置、使用不同的缓存名称,甚至利用Ehcache ...
### JavaWeb中Ehcache缓存配置详解 在JavaWeb应用开发中,缓存技术扮演着至关重要的角色,它能够显著提升应用性能和响应速度,减少数据库负担。Ehcache作为一款广泛使用的开源缓存解决方案,其高效、灵活的特性受到...
同时,可以通过`cache.use_second_level_cache`属性控制是否开启全局的二级缓存,其默认值为true。 ```xml <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider ``` 二、...
EHcache是一款广泛使用的Java缓存框架,尤其在SSH(Spring、Struts、Hibernate)这样的企业级开发环境中,它能够有效地提升应用的运行效率。本文将详细探讨如何在SSH架构中手动配置和使用EHcache。 首先,我们需要...
通过配置hibernate.cache.use_second_level_cache和hibernate.cache.region.factory_class等参数,可以启用并指定Ehcache作为Hibernate的缓存提供商。 **使用Ehcache的关键步骤:** 1. 添加Ehcache依赖到项目中。 2...
<property name="hibernate.cache.use_second_level_cache">true ``` - **指定缓存提供者**: 设置 Ehcache 为 Hibernate 的缓存提供者。 ```xml <property name="hibernate.cache.region.factory_class">org....
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 3. **实体类缓存配置**:对于...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 接着,为了使特定的实体类使用二...
"Spring Boot 集成 Ehcache 2.x 用于 Hibernate 二级缓存" 本篇文章主要介绍了如何在 Spring Boot 中集成 Ehcache 2.x 作为 Hibernate 的二级缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。 Ehcache ...
<property name="cache.use_second_level_cache">true ``` 为了利用注解来配置缓存,我们需要在实体类和关联映射上添加特定的注解。在Hibernate中,我们可以使用`@Cache`注解来标记实体类或集合属性,以便将其...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...