1.使用ehcache来提高系统的性能,现在用的非常多, 也支持分布式的缓存,在hibernate当中作为二级缓存的实现产品,可以提高查询性能。
2.在之前的struts2.12+Spring3.2+hibernate4.2集成系列的文章中已经集成了ehcache的jar包了, 在hibernate4.2当中可以找到ehcache-core-2.4.3.jar hibernate-ehcache-4.2.0.Final.jar
Hibernate 本身支持ehcahce的集成,提对他进行了集成的封装。
3. 在项目的src下面添加ehcache的配置文件ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- Subdirectories can be specified below the property e.g. java.io.tmpdir/one --> <diskStore path="java.io.tmpdir"/> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName) --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true" /> <cache name="org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="120" overflowToDisk="true" /> <!-- java文件注解查找cache方法名的策略:如果不指定java文件注解中的region="ehcache.xml中的name的属性值", 则使用name名为com.lysoft.bean.user.User的cache(即类的全路径名称), 如果不存在与类名匹配的cache名称, 则用 defaultCache 如果User包含set集合, 则需要另行指定其cache 例如User包含citySet集合, 则也需要 添加配置到ehcache.xml中 --> <cache name="copierCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
4. 在spring 集成hibernate 的配置文件中,添加如下配置
<!-- 开启查询缓存 --> <prop key="hibernate.cache.use_query_cache">true</prop> <!-- 开启二级缓存 --> <prop key="hibernate.cache.use_second_level_cache">true</prop> <!-- 高速缓存提供程序 --> <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <prop key="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory </prop>
5. Spring也使用ehcache, 所以也需要在spring配置文件中添加ehcache的配置
<!-- cacheManager, 指定ehcache.xml的位置 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <property name="shared" value="true"/> </bean>
由于hibernate 和spring都使用了ehcache, 可以由spring 或者hibernate 来管理ehcache,
使用spring管理ehcache的好处是可以使用依赖注入,可以在程序中注入使用,比较方便,因此采用spring管理ehcache比较好, 不过spring 和hibernate一起使用ehcache的话,会产生两个CacheManager,这样在分布式缓存中会有数据部同步的问题, 需要配置一下,spring和hibernate公用一个CacheManager。
EHCache升级到1.2之后,对原来CacheManager的实例化方式进行了一些修改。在之前的EHCache中,只会存在一个CacheManager的实例,所有使用EHCache组件的应用都共享这个实例。
而在EHCache 1.2之后,则提供了对多实例的支持。这样,在有多个功能模块同时使用EHCache的时候就要注意实例方式的选取了。
CacheManager是否为多实例取决于获得CacheManager的方法。如果使用构造方法来获得CacheManager对象的实例,那么就会获得一个新的CacheManager实例。而使用create()方法获得CacheManager实例的时候则会获得singleton的实例(如果还没有创建,它会自动地创建)。
现在,在一个应用中,默认情况下Hibernate会创建一个单独的CacheManager实例,如果在其他功能模块中也需要使用这个CahceManager实例,就需要使用EHCache所提供的singleton的CacheProvider。在Hibernate配置文件中的配置方法如下:
在配置hibernate的相关信息里面设置成单实例的CacheManager Factory
<!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <prop key="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory </prop>
spring 配置ehcache里面需要添加这个属性
<!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <property name="shared" value="true"/>
我们来看一下hibernate配置里面 SingletonEhCacheRegionFactory这个类的源代码, String configurationResourceName = null; if ( properties != null ) { configurationResourceName = (String) properties.get( NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME ); } if ( configurationResourceName == null || configurationResourceName.length() == 0 ) { manager = CacheManager.create(); REFERENCE_COUNT.incrementAndGet(); } 我们可以看到 会以默认的配置文件名称 创建一个CacheManager 对象,并且是以CacheManager.create()方法创建的, 说明只会产生一个CacheManager 实例。 再来看一下spring EhCacheManagerFactoryBean这个类的源码 private boolean shared = false; if (this.shared) { this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create()); } else { this.cacheManager = (is != null ? new CacheManager(is) : new CacheManager()); } 发现里面有个share的变量, 是否是共享使用CacheManger, 如果是true, 调用的是CacheManager.create()的方法创建CacheManager,false调用 new CacheManager方法会生成多个实例。 再看看CacheManger里面的create方法源码 private static volatile CacheManager singleton; if (singleton != null) { return singleton; } synchronized (CacheManager.class) { if (singleton == null) { LOG.debug("Creating new CacheManager with default config"); singleton = new CacheManager(); } else { LOG.debug("Attempting to create an existing singleton. Existing singleton returned."); } return singleton; } 可以看到CacheManager 定义的变量是static 的,create方面里面做了判断singleton 是否已经创建过。 配置好后, 把项目部署到tomcat上面,以debug的方式启动, 可以debug到源码可以观察CacheManger 的创建过程。 来检验一下缓存是否起作用了,添加一个User类 package com.lysoft.bean.user; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Table(name = "t_user") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="longTimeCache") public class User implements Serializable { private static final long serialVersionUID = 4239100106344646509L; private Integer id; private String userName; private String firstName; private Date createTime; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length = 50, nullable = false) public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Temporal(TemporalType.TIMESTAMP) public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } } 添加了一个注解@Cache, 指定缓存策略是读和写, 并指定使用的缓存区域 在ehcache中添加一个cache配置 <cache name="longTimeCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="900" overflowToDisk="true" /> 注意cache标签后面的 name 属性不要换行,换行了会有异常, 我上次不小心换行了, 查了半天文档都没有查到是什么问题,后来找个文件复制了一份对比才知道是换行引起的问题。 默认情况下二级缓存只会对load get 之类的方法缓存, 想list iterator 之类的方法也使用缓存 必须跟查询缓存一起使用, 在UserDaoImpl中重写listAll 方法。 public List<User> listAll() { return getSession().createQuery("from User").setCacheable(true).list(); }
在log4j 配置文件中 设置成debug级别
在网页中访问http://localhost:8080/SSH2/user/userList.do
第一次访问可以看到debug信息如下:
[2013-04-13 22:00:08.082] [DEBUG] [http-8080-1] [org.hibernate.cache.internal.StandardQueryCache:137] - Query results were not found in cache
[2013-04-13 22:00:08.113] [DEBUG] [http-8080-1] [org.hibernate.SQL:104] -
select
user0_.id as id1_0_,
user0_.createTime as createTi2_0_,
user0_.firstName as firstNam3_0_,
user0_.userName as userName4_0_
from
t_user user0_
缓存中找不到 Query results were not found in cache, 发出了查询sql。
第二次访问:
Debug日志
Checking query spaces are up-to-date: [t_user]
Returning cached query results
从缓存中找到了查询结果 直接返回了,没有发出查询sql。
相关推荐
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
自己整合的Spring+Hibernate+Ehcache 整合的项目 需要struts的自己添加。 里面带有注释,而且带Jar.写的很清楚,良心分。
maven环境下如何整合spring+hibernate+mysql+ehcache的方法
ssh,struts+hibernate+spring+ehcache集成
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
spring+springMVC+hibernate+cxf+quartz(定时器)+ehcache(缓存)
内含Springmvc+hibernate+Ehcache+shior+mysql+Oracle+fastjson,jar包整合,自己的加个jdk1.7便可使用。
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
Spring4MVC+Hibernate4+Freemarker+Ehcache+EasyUi整合,里面包含数据库,数据库使用的是Mysql
spring3 hibernate4 ehcache整合的实例
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.20 + Spring4.1.6 + Hibernate4.3.9。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
spring-expression-3.0.7.RELEASE.jar,spring-hibernate3.jar,spring-jms-3.0.7.RELEASE.jar,spring-tx-3.0.7.RELEASE.jar,spring-web-3.0.7.RELEASE.jar,sqljdbc4.jar,stax2-api-3.1.1.jar,velocity-1.7.jar,WHICH_...
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...
此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...
此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...
1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...
一、 项目名称:S2316S411H436 项目原型:Struts2.3.16 + Spring4.1.1 + ...(2) 提取四种Hibernate整合方案:四选一,请注释或删除其他方案 (3) 提供两种声明式事务管理器方案:二选一,请注释或删除其他方案
Spring-boot,hibernate ehcache 整合源码,可直接运行
此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...