springmvc+mybatis+ehcache配置详解

3 篇文章 0 订阅
1 篇文章 0 订阅

第一次在springmvc+mybatis 项目上整合ehcache,简单记录一下配置和遇到问题。
首先需要添加jar, maven项目直接贴配置了:

 <dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.6.9</version>
</dependency>

<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.0.3</version>
</dependency>

添加配置文件ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
     <!--磁盘存储配置:用来指定缓存在磁盘上的存储位置。
        可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)--> 
    <diskStore path="G:/cache" />

    <defaultCache overflowToDisk="true" eternal="false"
        timeToIdleSeconds="3600" timeToLiveSeconds="3600" maxElementsInMemory="10000"
        maxElementsOnDisk="10" diskPersistent="true"
        diskExpiryThreadIntervalSeconds="300" diskSpoolBufferSizeMB="1000"
        memoryStoreEvictionPolicy="LRU"
    />

    <!--
    name:Cache的唯一标识
    maxElementsInMemory:内存中最大缓存对象数
    maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
    eternal:Element是否永久有效,一但设置了,timeout将不起作用
    overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
    timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
    timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 
    diskPersistent:是否缓存虚拟机重启期数据
    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
    diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
     memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用) 
    -->

    <!-- service 缓存配置 -->
    <!--
        <cache name="test" overflowToDisk="true" eternal="false"
        timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
        maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
        diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
    -->
</ehcache> 

spring的配置文件中添加:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/cache 
                        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 自动扫描com.xxx下的service.impl -->
    <context:component-scan base-package="com.xxx">
    </context:component-scan>
    <!-- 使用 ehcache-core-2.4.6.jar 配置 -->
    <!-- 开启spring缓存 -->
<!--    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManagerFactory"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
        <property name="configLocation" value="classpath:conf/ehcache.xml" />
    </bean>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory" />
    </bean> -->
    <!-- 使用 ehcache-core-2.4.6.jar 配置 -->

    <!-- 使用 ehcache-core-2.6.9.jar 配置 -->
    <!-- ehcache-core jar 2.5.0以上版本需要加  p:shared="true"  -->
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">  
     <property name="configLocation" value="classpath:conf/ehcache.xml" />  
   </bean>
   <!-- 使用 ehcache-core-2.6.9.jar 配置 -->
   <!-- 注意classpath写完整 -->
   <!-- 以下配置略了 -->

Mapper文件中添加配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.XXX.service.container.dao.ContainerDao">
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 不输出日志  mapper的property设置优先于xml的-->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache">
    <property name="timeToIdleSeconds" value="5"/> 
    <property name="timeToLiveSeconds" value="5"/> 
    <property name="maxEntriesLocalHeap" value="1000"/>  
    <property name="maxEntriesLocalDisk" value="10000000"/>  
    <property name="memoryStoreEvictionPolicy" value="LRU"/>  
    </cache>
   <!-- 以下略了 -->

配置完毕,重启测试:
调用这个mapper的一个查询,然后手动操作修改数据库该表的信息,查询结果不变,查询缓存生效。
关于remove缓存问题,hibernate的项目是加注解@TriggersRemove,
mybatis是调用了配置这个mapper里的同一个表的非查询语句后缓存就清除了,重新查询的数据库(经测试有效,但是没有在官方文档查到详细解释)。
注意遇到的问题:当mapper文件里不设置cache的timeToLiveSeconds,timeToIdleSeconds时候,timeToIdleSeconds和timeToLiveSeconds时间不准确几分钟就访问数据库了,当在mapper文件里加上timeToIdleSeconds,timeToLiveSeconds 就OK了;
后来仔细想想应该mybatis和hibernate的还是有点区别的,mybatis的话就在mapper.xml文件多加上timeToIdleSeconds吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值