频道栏目
IT货架 > > 正文
spring中使用查询缓存
网友分享于:Jun 12, 2018 11:38:51 PM    来源: IT货架   

由于使用的是spring3所以一下配置都基于spring3.

 

先来看spring没有默认设置查询缓存的设置,spring的xml如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.directwebremoting.org/schema/spring-dwrhttp://www.directwebremoting.org/schema/spring-dwr-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"><context:property-placeholder location="classpath*:database.properties,classpath*:memcached.properties" /><!-- spring注释自动注入 --><context:annotation-config/><context:component-scan base-package="com.myweb" /><!--jbpm4.4工作流  --><bean/><bean factory-bean="springHelper"  factory-method="createProcessEngine" /><!-- dataSourceproxy 配置代理管理事务 --><bean /><!-- dataSource 多数据源支持 --><bean><property name="targetDataSources"><map key-type="java.lang.String"><entry key="dataSource" value-ref="dataSourceJDBC" /></map></property></bean><!-- c3p0数据源配置 --><bean p:driverClass="${jdbc.driverClass}" p:jdbcUrl="${jdbc.jdbcUrl}"p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"p:maxStatements="${c3p0.maxStatements}" lazy-init="true" /><!-- hibernate-spring 基本配置 --><bean><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop><prop key="hibernate.jdbc.fetch_size">50</prop><prop key="hibernate.jdbc.batch_size">25</prop><prop key="hibernate.cache.use_query_cache">true</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.max_fetch_depth">1</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop><prop key="hibernate.memcached.servers">${memcached.object.servers}</prop><prop key="hibernate.memcached.cacheTimeSeconds">86400</prop><prop key="hibernate.memcached.clearSupported">false</prop><prop key="hibernate.connection.release_mode">after_transaction</prop></props></property><property name="mappingLocations"><list><value>classpath*:com/<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">/modal/hibernate/*.hbm.xml</value></span><value>classpath*:jbpm.repository.hbm.xml</value><value>classpath*:jbpm.execution.hbm.xml</value><value>classpath*:jbpm.history.hbm.xml</value><value>classpath*:jbpm.task.hbm.xml</value><value>classpath*:jbpm.identity.hbm.xml</value></list></property><!-- 使用TransactionAwareDataSourceProxy管理事务与ibatis处于同一事务管理下 --><property name="useTransactionAwareDataSource" value="true"></property></bean><!-- mybatis-spring 配置 --><bean><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath*:com/<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">/ibatis/mapper/*.xml"/></span></bean><bean><property name="basePackage" value="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.dao.ibatis" /></span></bean><!-- spring transaction 事务管理 --><bean><property name="dataSource" ref="dataSource" /></bean><tx:advice transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:advisor advice-ref="txManager" pointcut="execution(* com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.*service..*(..))" order="0" /></span></aop:config><!-- memcache-java --><bean class="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.service.ObjectCache"></span><property name="memCachedClient"><props><prop key="memcached.servers">${memcached.object.servers}</prop><prop key="memcached.weights">${memcached.object.weights}</prop><prop key="memcached.failover">${memcached.failover}</prop><prop key="memcached.initConn">${memcached.initConn}</prop><prop key="memcached.minConn">${memcached.minConn}</prop><prop key="memcached.maxConn">${memcached.maxConn}</prop><prop key="memcached.maintSleep">${memcached.maintSleep}</prop><prop key="memcached.nagle">${memcached.nagle}</prop><prop key="memcached.socketTO">${memcached.socketTO}</prop><prop key="memcached.aliveCheck">${memcached.aliveCheck}</prop></props></property></bean></beans>

 主要的BaseHDao配置如下:

package com.myweb.dao.hibernate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;/*** @author jsczxy2**/@Repositorypublic class BaseHDao extends HibernateDaoSupport {Log log = LogFactory.getLog(getClass());@Autowiredprivate SessionFactory sessionFactory;}

 再来看spring默认设置查询缓存,其实差异就在hibernateTemplate和hibernateTemplate里的cacheQueries设置为true!

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.directwebremoting.org/schema/spring-dwrhttp://www.directwebremoting.org/schema/spring-dwr-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"><context:property-placeholder location="classpath*:database.properties,classpath*:memcached.properties" /><!-- spring注释自动注入 --><context:annotation-config/><context:component-scan base-package="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">" /></span><!--jbpm4.4工作流  --><bean/><bean factory-bean="springHelper"  factory-method="createProcessEngine" /><!-- dataSourceproxy 配置代理管理事务 --><bean /><!-- dataSource 多数据源支持 --><bean class="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.support.DynamicDataSource"></span><property name="targetDataSources"><map key-type="java.lang.String"><entry key="dataSource" value-ref="dataSourceJDBC" /></map></property></bean><!-- c3p0数据源配置 --><bean p:driverClass="${jdbc.driverClass}" p:jdbcUrl="${jdbc.jdbcUrl}"p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"p:maxStatements="${c3p0.maxStatements}" lazy-init="true" /><!-- hibernate-spring 基本配置 --><bean><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop><prop key="hibernate.jdbc.fetch_size">50</prop><prop key="hibernate.jdbc.batch_size">25</prop><prop key="hibernate.cache.use_query_cache">true</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.max_fetch_depth">1</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop><prop key="hibernate.memcached.servers">${memcached.object.servers}</prop><prop key="hibernate.memcached.cacheTimeSeconds">86400</prop><prop key="hibernate.memcached.clearSupported">false</prop><prop key="hibernate.connection.release_mode">after_transaction</prop></props></property><property name="mappingLocations"><list><value>classpath*:com/<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">/modal/hibernate/*.hbm.xml</value></span><value>classpath*:jbpm.repository.hbm.xml</value><value>classpath*:jbpm.execution.hbm.xml</value><value>classpath*:jbpm.history.hbm.xml</value><value>classpath*:jbpm.task.hbm.xml</value><value>classpath*:jbpm.identity.hbm.xml</value></list></property><!-- 使用TransactionAwareDataSourceProxy管理事务与ibatis处于同一事务管理下 --><property name="useTransactionAwareDataSource" value="true"></property></bean><!-- hibernateTemplate 并使用查询缓存设置--><bean><property name="sessionFactory"><ref bean="sessionFactory"/></property><property name="cacheQueries" value="true"></property></bean><!-- mybatis-spring 配置 --><bean><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath*:com/<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">/ibatis/mapper/*.xml"/></span></bean><bean><property name="basePackage" value="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.dao.ibatis" /></span></bean><!-- spring transaction 事务管理 --><bean><property name="dataSource" ref="dataSource" /></bean><tx:advice transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:advisor advice-ref="txManager" pointcut="execution(* com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.*service..*(..))" order="0" /></span></aop:config><!-- memcache-java --><bean class="com.<span style="font-size: 1em; line-height: 1.5;">myweb</span><span style="font-size: 1em; line-height: 1.5;">.service.ObjectCache"></span><property name="memCachedClient"><props><prop key="memcached.servers">${memcached.object.servers}</prop><prop key="memcached.weights">${memcached.object.weights}</prop><prop key="memcached.failover">${memcached.failover}</prop><prop key="memcached.initConn">${memcached.initConn}</prop><prop key="memcached.minConn">${memcached.minConn}</prop><prop key="memcached.maxConn">${memcached.maxConn}</prop><prop key="memcached.maintSleep">${memcached.maintSleep}</prop><prop key="memcached.nagle">${memcached.nagle}</prop><prop key="memcached.socketTO">${memcached.socketTO}</prop><prop key="memcached.aliveCheck">${memcached.aliveCheck}</prop></props></property></bean></beans>

 

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;/*** @author jsczxy2**/@Repositorypublic class BaseHDao extends HibernateDaoSupport {Log log = LogFactory.getLog(getClass());@Autowiredprivate HibernateTemplate hibernateTemplate;}

 这样可以看出使用spring的HibernateTemplate 来设置cacheQueries为true就是设置spring默认打开查询缓存。查询缓存会缓存查询语句的结果,其生命周期是到相关映射实体对象有变动为止,一旦有变动则会重新查询后缓存。

 

 

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1