Eddyer
发下工资正心凉,一紧张,码全忘。似曾相识,何用却不详。设计模式两茫茫,看代码,泪千行。步出小窝见同事,都一样,很受伤。如此工资,无颜见嫩娘。只待晚上交钱日,接绳套,系房梁。

1,几点说明:

1、对于包的说明:

ehcache-core-2.6.9.jar

mybatis-ehcache-1.0.3.jar

slf4j-api-2.2.jar

slf4j-log4j12-2.2.jar

2、对于自动加载ehcache.xml的说明:

此版本的ehcache会自动到classpath下去找ehcache.xml,命名也是必须这样命名。

 

2,使用:

1、导包。

 

 <!--ehcache-->
    <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-->

 

2、注意,所缓存的bean需要implements Serializable 序列化接口。

 

public class User implements Serializable {

    private int id;

    private String mobile;

    private String password;
    
//省略get,set

 

3、在classpath下创建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" updateCheck="false">
    <!--
        maxElementsInMemory:缓存中允许创建的最大对象数
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
        timeToIdleSeconds:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔,
                            如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存自创建日期起至失效时的间隔时间,
                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        overflowToDisk:内存不足时,是否启用磁盘缓存。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。

        timeToLiveSeconds=3600并且timeToIdleSeconds=600表示:此缓存最多可以存活60分钟,如果期间超过10分钟未访问,那么此缓存失效!
    -->
    <!--<diskStore path="java.io.tmpdir"/>-->
    <!--设置缓存目录
 -->
    <diskStore path="C:\ehcache"></diskStore>
    <defaultCache maxElementsInMemory="3000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600"
                  overflowToDisk="true" maxEntriesLocalHeap="1000" memoryStoreEvictionPolicy="LRU"
                  maxEntriesLocalDisk="10000000">
        <!--设置了分布式缓存-->
        <!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"-->
                                   <!--properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,-->
                                   <!--replicateUpdatesViaCopy=false, replicateRemovals=true"/>-->
    </defaultCache>
</ehcache>

4、mapperxml中加入

 

<?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.eddy.webapp.mapper.PassengerMapper">

    <!-- 自定义缓存 <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

....
</mapper>

 

一切ok。

 

posted on 2015-08-29 12:34  Eddyer  阅读(205)  评论(0)    收藏  举报