个性化阅读的过去和未来【三】

来源:互联网 发布:苹果gsx查询软件 编辑:程序博客网 时间:2024/04/29 09:23

总体思路:使用Hibernate3.2的二级缓存,解决使用频率最多的find(Class clz,Object id)方法的缓存。

一、使用Hibernate3.2的二级缓存功能,只开取针对id查找实体的缓存,不开启基于list查询的缓存。
技术调整如下:
1、升级Spring2的版本号,升级为2.06,更新spring.jar、spring-aspects.jar、spring-mock.jar,为了使用spring modules中提供的cache功能,增加了spring-modules-cache.jar。以上包已经添加到svn中。

2、修改jpa-base.xml中的entityManagerFactory Bean的配置信息,把对loadTimeWeaver属性的注入注释掉。

<BEAN class=org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean name="entityManagerFactory">BEAN>  
  
<property name="persistenceXmlLocation" value="classpath:persistence.xml">property>  
  
<property name="dataSource" ref="dataSource">property>  
  
<property name="jpaVendorAdapter">  
   
<BEAN class=org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>  
    
="database" value="MYSQL">property>  
    
<property name="showSql" value="false">property>  
    
<property name="generateDdl" value="false">property>  
 
  
property>  
  
  


在persistence.xml文件中,添加如下的配置信息,开启Hibernate的二级缓存: 

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider">property>  
<property name="hibernate.cache.use_query_cache" value="false">property>

3、在src目录下增加ehcache.xml,设置cache的配置信息,默认情况下可以考虑给一些常用的Entity类设置一个单独的cache区域,如下所示:

<CACHE name="com.easyjf.security.Resource" maxelementsinmemory="1000" eternal="false" overflowtodisk="false" memorystoreevictionpolicy="LFU">CACHE>  


4、然后修改Domain对象,对于要使用缓存的的Entity,在类声明前加上如下的标签:<BR>@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE),此处usage的值还需要根据应用情况进行必要的调整。<BR> P><P>5、暂时使用ehcache作为Spring modules的cache。在ehcache.xml文件中继续配置用于为spring提供方法调用结果的缓存。大致如下:

  
      
 
<CACHE name="CMSCache" maxelementsinmemory="5000" eternal="false" overflowtodisk="true" memorystoreevictionpolicy="LFU">CACHE>  
 
<CACHE name="ECCache" maxelementsinmemory="5000" eternal="false" overflowtodisk="true" memorystoreevictionpolicy="LFU">CACHE>


6、然后在具体的Service类中配置缓存。使用了AOP,需要修改spring的配置文件,比如cms-core.xml中为了给ICmsManageService的get*方法添加结果缓存,调整如下:

<EHCACHE:PROXY id=cmsManageService>     
 
=com.easyjf.cms.service.impl.CmsManageServiceImpl>     
  
="newsAuthorDao" ref="newsAuthorDao">property>  
  
<property name="newsDocDao" ref="newsDocDao">property>  
  
<property name="newsDirDao" ref="newsDirDao">property>  
  
<property name="newsSourceDao" ref="newsSourceDao">property>  
  
<property name="reviewDao" ref="newsRivewDao">property>  
    
 
<EHCACHE:CACHING cachename="CMSCache" methodname="get*">EHCACHE:CACHING>  
 
<EHCACHE:FLUSHING methodname="update*" cachenames="CMSCache" when="before">EHCACHE:FLUSHING>  
 
EHCACHE:PROXY>


调整前对照: 

<BEAN class=com.easyjf.cms.service.impl.CmsManageServiceImpl id=cmsManageService>     
  
="newsAuthorDao" ref="newsAuthorDao">property>  
  
<property name="newsDocDao" ref="newsDocDao">property>  
  
<property name="newsDirDao" ref="newsDirDao">property>  
  
<property name="newsSourceDao" ref="newsSourceDao">property>  
  
<property name="reviewDao" ref="newsRivewDao">property> 


为了让Spring配置文件能识别并处理<EHCACHE:XXX>这个标签,需要在beans中进行schem声明,如下所示:<BR>另外在spring配置文件中再增加<EHCACHE:CONFIG configlocation="classpath:ehcache.xml">EHCACHE:CONFIG>,以便Spring能找到Cache配置文件。EHCACHE:XXX>

<BEANS xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">BEANS>  
   


7、以上只是基本的配置,cache运行的具体性能,还需要根据实际的数据量及并发量等进行更加细致的调整。

8、另外EasyJWeb还将会提供一个页面结果缓存,直接缓存Action的执行结果,这样就可以解决访问得最多,属于严重性能瓶颈的问题。比如ec-brand.ejf、index.ejf等。这一功能将在9月15号前推出。

9、一些必要的页面,需要增加静态文件生成功能。(逐渐调整)

注释:

  由于发现Spring2.06版本与当前我们使用的版本存在一些冲突。而且跟EasyJWeb中的maven混合编译的时候存在一些问题,因此暂时取消使用Spring的方法Cache,而只使用Hibernate的Cache及EasyJWeb的缓存配合。EasyJWeb的缓存简单机制已经实现,直接在基于AbstractCmdAction的Action中,在要缓存的Command中使用缓存标签@WebCache即可。




原创粉丝点击