随笔 - 115  文章 - 481  trackbacks - 0
<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(19)

随笔档案(115)

文章档案(4)

新闻档案(1)

成员连接

搜索

  •  

最新评论

阅读排行榜

评论排行榜

总体思路:使用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></BEAN>  
    
<property name="database" value="MYSQL"></property>  
    
<property name="showSql" value="false"></property>  
    
<property name="generateDdl" value="false"></property>  
 
  
</property>  
  
<!--   
  <property name="loadTimeWeaver">  
   <bean  
    class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />  
  </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><!--暂时不开query cache-->

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

<CACHE name="com.easyjf.security.Resource" maxelementsinmemory="1000" eternal="false" overflowtodisk="false" memorystoreevictionpolicy="LFU"></CACHE>  
<!--配置信息的说明如下:   
缺省缓存配置。CacheManager 会把这些配置应用到程序中。   
        下列属性是 defaultCache 必须的:   
  
        maxInMemory           - 设定内存中创建对象的最大值。   
        eternal                        - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超   
                                              时限制且元素永不消亡。   
        timeToIdleSeconds  - 设置某个元素消亡前的停顿时间。   
                                              也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。   
                                              这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则   
                                              设置该属性也无用)。   
                                              如果该值是 0 就意味着元素可以停顿无穷长的时间。   
        timeToLiveSeconds - 为元素设置消亡前的生存时间。   
                                               也就是一个元素从构建到消亡的最大时间间隔值。   
                                               这只能在元素不是永久驻留时有效。   
        overflowToDisk        - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘   
                                               上。   
        
-->


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

<!--以下是为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>     
 
<BEAN class=com.easyjf.cms.service.impl.CmsManageServiceImpl></BEAN>     
  
<property name="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></BEAN>     
  
<property name="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即可。

posted on 2007-11-28 16:53 简易java框架 阅读(1272) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: