zoukankan      html  css  js  c++  java
  • 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秒刷新。

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

  • 相关阅读:
    Microjs: 超棒的迷你框架和迷你类库搜罗工具
    本周推荐7款CSS3实现的动态特效
    Bootstrap3.1开发的响应式个人简历模板
    10分钟,利用canvas画一个小的loading界面
    四款超棒的jQuery数字化签名插件
    搜索引擎优化网页设计:最佳实践
    推荐超实用的8款jQuery插件
    9款HTML5实现的超酷特效
    想成为程序猿?28个在线学习网站让你变身齐天大圣!
    推荐7款超棒的单页面网站设计模板。关键是!免费!!
  • 原文地址:https://www.cnblogs.com/acehalo/p/3902883.html
Copyright © 2011-2022 走看看