spring+mybatis+ehcache配置缓存

首先是mybatis+ehcache,转自:http://www.cnblogs.com/little-fly/p/6251439.html

/***************************************************************************************************************************************************************************************************************************************************************************************************************************/

mybatis整合ehcache缓存框架的使用

  mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存。

1.开启mybatis的二级缓存

  在核心配置文件SqlMapConfig.xml中加入

1 <settings>
2 <!-- 开启二级缓存 -->
3 <setting name="cacheEnabled" value="true"/>
4 </settings>

 


2.导入ehcache相关jar包

ehcache-core-2.6.5.jar

mybatis-ehcache-1.0.2.jar


3.在classpath下加入ehcache.xml文件

复制代码
 1 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2 xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 3 <diskStore path="F:\develop\ehcache" />
 4 <defaultCache
 5 maxElementsInMemory="1000"
 6 maxElementsOnDisk="10000000"
 7 eternal="false"
 8 overflowToDisk="false"
 9 timeToIdleSeconds="120"
10 timeToLiveSeconds="120"
11 diskExpiryThreadIntervalSeconds="120"
12 memoryStoreEvictionPolicy="LRU">
13 </defaultCache>
14 </ehcache> 
复制代码

 

属性说明:
 diskStore:指定数据在磁盘中的存储位置。
 defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
以下属性是必须的:
 maxElementsInMemory - 在内存中缓存的element的最大数目 
 maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
 eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
 overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
以下属性是可选的:
 timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
 timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
 diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
 diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
 memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)


4.在UserMapper.xml中开启二缓存,UserMapper.xml下的sql执行完成会存储到它的缓存区域(HashMap)

  根据需求调整缓存参数:

复制代码
1 <cache type="org.mybatis.caches.ehcache.EhcacheCache" >
2 <property name="timeToIdleSeconds" value="3600"/>
3 <property name="timeToLiveSeconds" value="3600"/>
4 <!-- 同ehcache参数maxElementsInMemory -->
5 <property name="maxEntriesLocalHeap" value="1000"/>
6 <!-- 同ehcache参数maxElementsOnDisk -->
7 <property name="maxEntriesLocalDisk" value="10000000"/>
8 <property name="memoryStoreEvictionPolicy" value="LRU"/>
9 </cache>
复制代码

 

    <!--mybatis ehcache缓存配置 -->
    <!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
     <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 

 

log4j打印如下日志,说明缓存开启成功:

DEBUG [main] - Cache Hit Ratio [cn.itcast.jdbc.mapper.UserMapper]: 0.0


应用场景:
  对于访问多的查询请求且用户对查询结果实时性要求不高,此时可采用mybatis二级缓存技术降低数据库访问量,提高访问速度,业务场景比如:耗时较高的统计分析sql、电话账单查询sql等。
实现方法如下:通过设置刷新间隔时间,由mybatis每隔一段时间自动清空缓存,根据数据变化频率设置缓存刷新间隔flushInterval,比如设置为30分钟、60分钟、24小时等,根据需求而定。


局限性:
  mybatis二级缓存对细粒度的数据级别的缓存实现不好,比如如下需求:对商品信息进行缓存,由于商品信息查询访问量大,但是要求用户每次都能查询最新的商品信息,此时如果使用mybatis的二级缓存就无法实现当一个商品变化时只刷新该商品的缓存信息而不刷新其它商品的信息,因为mybaits的二级缓存区域以mapper为单位划分,当一个商品信息变化会将所有商品信息的缓存数据全部清空。解决此类问题需要在业务层根据需求对数据有针对性缓存。

  缓存都是实现了Cache这个接口.....

  public class EhCache implements Cache{}

作者:little飞 出处:http://www.cnblogs.com/little-fly/ 欢迎转载 也请保留这段声明 谢谢!

 

/*******************************************************************************************************************************************************************************************************************************************************************************************************************/

 

如果要让spring进行管理,只需在spring配置文件中配置:

<!-- 使用ehcache缓存 -->

<?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:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">

<!-- 开启spring缓存 -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>

</beans>

这里需要注意一点,如果你用的 ehcache-core包是2.5以上的,还需要加上<property name="shared" value="true"></property>,不然启动会报错。因为2.5之前的ehCacheManager不是单例的,2.5之后就是了。

 

 

如果某个语句不想使用缓存,如分页查询,则可用:

<select id="getOrder" parameterType="int" resultType="TOrder"  useCache="false">
        ...
    </select>

useCache="false"表示该select语句不使用缓存(即使xml最开头的全局cache启用)

 

 

 

posted @ 2017-08-16 17:07  葱香排骨面  阅读(219)  评论(0编辑  收藏  举报