ibatis自带缓存及reids缓存

一、直接使用ibatis默认的缓存配置方式

配置缓存模块cacheModel,如:

SqlMap中设置:

<typeAlias alias="rc" type="com.xcm.fund.core.test.RedisCache"/>

<cacheModel id="usercache" type="rc" readOnly="true" serialize="false">

         <flushInterval hours="24" /><!-- hours,minutes,seconds,milliseconds -->

         <flushOnExecute statement="user.inser" />

         <flushOnExecute statement="user.update" />

         <flushOnExecute statement="user.del" />

         <property value="1000" name="size" />

</cacheModel>

使用时加上userCache即可。

<select id=”find” resultClass=”User” cacheModel=”userCache”>……</select>

sqlMapConfig中设置:

<settings cacheModelsEnabled="true" enhancementEnabled="true"

                   lazyLoadingEnabled="true" useStatementNamespaces="true" />

其中rc为自定义类型,用到第三方缓存时需要定义。如上面用到redis。如果用ibatis自带的缓存类型的话,则直接写即可。如LRU,具体可查询相关资料。

flushInterval为间隔时间刷新缓存。可定义小时,分钟,等

flushOnExecute为调用方法后刷新缓存。如果在sqlMapConfig下未配置sqlMap的话会失效。具体解决办法可参考【flushOnExecute失效解决方法】

cacheModelsEnabled需要设置为true。

二、使用第三方缓存

需要设置type类型为自定义的。如上面的rc,com.xcm.fund.core.test.RedisCache的具体方法参考相关文件(配置redis等相关)。

其他的设置和使用ibatis自带的没区别。

三、flushOnExecute失效解决方法

一般情况下,ibatis和spring结合使用时sqlmap都是通过spring管理的,因此sqlmap文件一般不放在sqlMapConfig下面。这个时候flushOnExecute会失效,解决办法是重写SqlMapClientFactoryBean方法。如下面的配置:

<bean id="sqlMapClientPoss" class="com.common.item.base.util.MySqlMapClientFactoryBean">

                   <property name="configLocation">

                            <value>classpath:config/ibatis/context-fundout-sqlmap-config.xml</value>

                   </property>

                   <property name="mappingLocations">

                            <value>classpath*:config/ibatis/mappings/**/*.xml</value>

                   </property>

                   <property name="dataSource">

                            <ref bean="xcmDataSource" />

                   </property>

                   <property name="lobHandler" ref="lobHandler"></property>

</bean>

类com.common.item.base.util.MySqlMapClientFactoryBean即为重写的,原始使用的方法为:org.springframework.orm.ibatis.SqlMapClientFactoryBean。重写的关键代码为:buildSqlMapClient方法中添加如下代码:

//*************其实只改这一点而已,为了方便他人,全source贴出************** 

        //为了取sqlMapConfig,反射private的field yanghuiping

                   try {

                            Field stateField = configParser.getClass().getDeclaredField("state");

                            stateField.setAccessible(true);

                            XmlParserState state = (XmlParserState) stateField.get(configParser);

                            SqlMapConfiguration sqlMapConfig = state.getConfig();

                            // 反射取设置cache的方法,执行

                            Method wireUpCacheModels = sqlMapConfig.getClass().getDeclaredMethod("wireUpCacheModels");

                            wireUpCacheModels.setAccessible(true);

                            wireUpCacheModels.invoke(sqlMapConfig);

                   } catch (Exception e) {

                            e.printStackTrace();

                            System.out.println("---yhp-------------");

                   }

四、使用第三方redis的目的

如果使用redis默认的缓存方式,如果在多台服务器上部署的话,会引起一系列问题,如两天同时缓存了,第一个更新了,第二个没有更新,这样如果用第二个查询的话还是用的未更新的缓存,这样会有问题。

也就是两个服务器上各自缓存,互不干涉了。这样会有问题。分布式部署,同一个服务部署到几台服务器上。

原文地址:https://www.cnblogs.com/yanghuiping/p/6406554.html