springmvc+mybatis+ehcache配置详解
来源:互联网 发布:学生空间七天网络答案 编辑:程序博客网 时间:2024/06/05 05:03
第一次在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
- springmvc+mybatis+ehcache配置详解
- springmvc-mybatis-ehcache配置
- springMVC+mybatis+ehcache详细配置
- springMVC+mybatis+ehcache详细配置
- springMVC+mybatis+ehcache详细配置
- springMVC+mybatis+ehcache详细配置
- springMVC+mybatis+ehcache详细配置
- springMVC+mybatis+ehcache缓存配置实践
- springMVC+mybatis配置详解
- springMVC+mybatis+spring+ehcache
- springmvc+ehcache详解
- 框架,springmvc,mybatis,ehcache,源码
- maven + springmvc + mybatis + ehcache 搭建
- mybatis-ehcache配置
- mybatis配置ehcache
- mybatis配置ehcache缓存
- mybatis-ehcache配置
- spring、springmvc、mybatis整合文件配置详解
- C语言再学习 -- 声明与定义
- Android 存储数据的五种方法总结。。。。。。。
- 制作自己的IOC框架
- zabbix系列(七)zabbix3.0添加对tcp连接数及状态的监控
- AVL树(平衡二叉树)及其实现
- springmvc+mybatis+ehcache配置详解
- LCS(Longest Common Sequence)最长公共子序列算法
- C++开发预定义宏——以VS2010为例
- 浙大PAT甲级 1040
- 占位
- leetcode-java-279. Perfect Squares
- Spring 与 Struts2的区别
- listview+checkbox实现单选
- vijos1144&&bzoj1596(树形dp,树上最小控制集