mybatis增加ehcache缓存

之前SpringMvc和mybatis整合的例子:http://www.cnblogs.com/acehalo/p/3901809.html

为mybatis添加ehcache缓存,参考的http://www.verydemo.com/demo_c180_i57073.html

pom添加:

        <!-- ehchache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.0</version>
        </dependency>

之后新建ehcache.xml,放在classpath下,就是和applicationContext.xml同级目录下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
    <defaultCache overflowToDisk="true" eternal="false"
        maxElementsInMemory="1" />
    <diskStore path="S:/cache" />
</ehcache>

之后再mapper下添加:

<mapper namespace="com.hi.test.mapper.UserMapper">下面添加:

<!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> //最普通的设置,沿用全局设置 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" > 
    <property name="timeToIdleSeconds" value="3600"/><!--1 hour-->
    <property name="timeToLiveSeconds" value="3600"/><!--1 hour-->
    <property name="maxEntriesLocalHeap" value="1000"/>
    <property name="maxEntriesLocalDisk" value="10000000"/>
    <property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
<!--
配置这个mapper使用LRU替换策略。
(个人比较赞同这种配置,因为每个表的数据都不一样,有一些需要经常更新,有得可能某几个字段需要经常做连接,
使用一样的cache不太合适)
--> 

这样就添加完了,之后看下效果:

首先把TxTestService类下的throw new RuntimeException();注释掉,因为现在需要进行批量插入数据。

打开http://localhost:8080/Test/druid/sql.html这个界面看sql查询

然后打开http://localhost:8080/Test/TxTest.do 进行插入数据

之后能在druid的监控页面看到执行了100次insert。

然后打开http://localhost:8080/Test/indexList.do

之后能在druid的监控页面看到执行了1次select。

之后关闭浏览器,或者新开小号窗口,隐私窗口之类的再次访问

http://localhost:8080/Test/indexList.do

发现在druid的监控界面依然只有1次select。

再打开http://localhost:8080/Test/TxTest.do 进行插入数据。

druid监控的insert变为200次。

访问http://localhost:8080/Test/indexList.do

之后看druid可以发现再次执行了1次select,总共select变为了2次

再打开http://localhost:8080/Test/indexList.do

select次数依旧是2次。

这样应该就能说明缓存正常开启了。

现在还有个问题,暂时还没解决,缓存脏读取的问题:

如果我在另一个mapper对user表进行插入操作:即 新建一个bean取名UserNew,一个Mapper取名UserNewMapper,一个UserNewMapper.xml,

如果我利用这个新的对象对user表进行操作。

那么此时原来的UserMapper依旧是读取缓存的数据,无法读取实时的数据。

继而想到,表操作很多都是关联操作,这样的缓存肯定存在一定问题,

寻求解决方案中。

又试了下:

不知道为什么,感觉mapper里的cache配置没有用

<property name="timeToLiveSeconds" value="3600"/> 把value设置成10 也不起作用
加上<property name="eternal" value="false"/>也不起作用
最后把mapper里的cache配置删了
只留下:

<cache type="org.mybatis.caches.ehcache.LoggingEhcache" > </cache>

即把配置放到ehcache.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
    <defaultCache overflowToDisk="true" eternal="false"
        maxElementsInMemory="1" 
        timeToLiveSeconds="10"
        maxEntriesLocalHeap="1000"
        maxEntriesLocalDisk="10000000"
        memoryStoreEvictionPolicy="LRU"
    
        />
    <diskStore path="S:/cache" />
</ehcache>

这样timeToLiveSeconds就起作用了。

10秒刷新。

暂时觉得就先这样解决缓存脏读取的问题吧

原文地址:https://www.cnblogs.com/acehalo/p/3902883.html