mybatis 配置ehcache 验证二级缓存是否起作用

按照https://my.oschina.net/u/1469930/blog/388004 配置好,但是不知道配置的二级缓存是否起作用;

用不同的浏览器打开同一个链接,调用相同的方法,tomcat容器的session会话不同,但是相同的查询,只像数据库请求了一次SQL.

准备工作:

1.打开mysql的log,windows和linux不一样哈,目的是为了查看请求时,向数据库发了SQL查询没;

windows

[mysqld] 
# [mysqld] 下添加日志在H盘
log-error="H:\mysql_log\error.log"  
log="H:\mysql_log\mysql.log"  
long_query_time=2  
log-slow-queries="H:\mysql_log\slowquery.log" 

 linux

# 在[mysqld]  
#log  
log-error=/usr/local/mysql/log/error.log  
log=/usr/local/mysql/log/mysql.log  
long_query_time=2  
log-slow-queries= /usr/local/mysql/log/slowquery.log 

2.分别在没有配置缓存和配置了缓存的情况下,不同浏览器访问相同链接,代码中打印时间,同时观察mysql的日志变化,linux用tail -f ,windows用监控文本变化的工具,比如UE,notepade++(高版本),wintalk.ext

得出结论

1.在mapper中关闭了2级缓存
	浏览器请求不同的session;点击一次发送一次sql到数据库;SqlSession一致;

2.在mapper中开启了2级缓存
	不同浏览器相同请求,数据库日志只有第一次发出了sql查询,第一次耗时多,以后几乎耗时为0毫秒;

 3.配置:

pom.xml

		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.4.6</version>
		</dependency>
		<dependency>
		    <groupId>org.mybatis.caches</groupId>
		    <artifactId>mybatis-ehcache</artifactId>
		    <version>1.0.1</version>
		</dependency>		
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context-support</artifactId>
		    <version>4.2.4.RELEASE</version>
		</dependency>

 ehcache.xml

<?xml version="1.0" encoding="utf-8"?>   
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">       
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache  maxElementsInMemory="100000" maxElementsOnDisk="300000" eternal="false" timeToIdleSeconds="1800" 
               timeToLiveSeconds="3600" overflowToDisk="true" memoryStoreEvictionPolicy="LFU"/>  
    <!--           
  	  配置自定义缓存          
    	maxElementsInMemory			|	设置基于内存的缓存可存放对象的最大数目  
    	maxElementsOnDisk			|	设置基于硬盘的缓存可存放对象的最大数目
    	eternal					|	如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false
    	timeToIdleSeconds			|	设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了就clear cache,
    									只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态
    	timeToLiveSeconds			|	设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,
    									这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,
                                                        则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。
    	overflowToDisk				|	如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。
    							注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
    	memoryStoreEvictionPolicy	        |	        缓存对象清除策略。有三种:
    										FIFO ,first in first out 
    										 LFU , Less Frequently Used ,缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存,使用得少的就会被清出
    										 LRU ,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓
                                                        存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
    -->   
</ehcache>

 **Mapper.xml

	<!-- ehcache缓存 -->
<!-- 	<cache type="org.mybatis.caches.ehcache.EhcacheCache" /> -->
	<cache type="org.mybatis.caches.ehcache.LoggingEhcache">  
	    <property name="timeToIdleSeconds" value="3600"/>
	    <property name="timeToLiveSeconds" value="3600"/>  
	    <property name="maxEntriesLocalHeap" value="1000"/>  
	    <property name="maxEntriesLocalDisk" value="10000"/>  
	    <property name="memoryStoreEvictionPolicy" value="LRU"/>  
	</cache>

 注入spring

	<!-- 使用ehcache缓存 -->    
   <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
     <property name="configLocation" value="classpath:ehcache.xml" />  
   </bean>

 可以用flushcache() userCache()配合着查看区别。

猜你喜欢

转载自tangyiss.iteye.com/blog/2326340