`
fishergay
  • 浏览: 33129 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring+hibernate整合ehcache

    博客分类:
  • SSH
 
阅读更多

1.使用ehcache来提高系统的性能,现在用的非常多, 也支持分布式的缓存,在hibernate当中作为二级缓存的实现产品,可以提高查询性能。

2.在之前的struts2.12+Spring3.2+hibernate4.2集成系列的文章中已经集成了ehcachejar包了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,这样在分布式缓存中会有数据部同步的问题, 需要配置一下,springhibernate公用一个CacheManager

 

EHCache升级到1.2之后,对原来CacheManager的实例化方式进行了一些修改。在之前的EHCache中,只会存在一个CacheManager的实例,所有使用EHCache组件的应用都共享这个实例。

而在EHCache 1.2之后,则提供了对多实例的支持。这样,在有多个功能模块同时使用EHCache的时候就要注意实例方式的选取了。

CacheManager是否为多实例取决于获得CacheManager的方法。如果使用构造方法来获得CacheManager对象的实例,那么就会获得一个新的CacheManager实例。而使用create()方法获得CacheManager实例的时候则会获得singleton的实例(如果还没有创建,它会自动地创建)。

现在,在一个应用中,默认情况下Hibernate会创建一个单独的CacheManager实例,如果在其他功能模块中也需要使用这个CahceManager实例,就需要使用EHCache所提供的singletonCacheProvider。在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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics