【Cache】Ehcache相关

准备学Redis来,想想还是先看看Ehcache

Ehcache可以和Spring整合使用,也可以作为Mybatis的二级缓存,两个配置一开始看得我也是一脸懵逼,混杂在一起了

Spring-Ehcache

  1. pom.xml,配置好用的包
        <!-- spring-ehcache 相关包 -->
        <dependency>
            <groupId>com.googlecode.ehcache-spring-annotations</groupId>
            <artifactId>ehcache-spring-annotations</artifactId>
            <version>1.2.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.2</version>
            <type>pom</type>
        </dependency>
  1. ehcache.xml,配置缓存存储路径以及缓存大小什么的(好像不给写中文注解在这个文件里)
<?xml version="1.0" encoding="utf-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
         monitoring="autodetect" dynamicConfig="true">

    <diskStore path="${java.io.tmpdir}/${system.project_name}/cache"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="30"
            timeToLiveSeconds="30"
            overflowToDisk="false"/>

    <cache name="baseCache"
           maxElementsInMemory="10000"
           eternal="true"
           overflowToDisk="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU" />
</ehcache>
  1. spring-cache.xml,配置整合Spring和Ehcache
<?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:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 缓存配置(两种) -->
    <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
    <!--
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
            </set>
        </property>
    </bean>
     -->
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:/config/ehcache.xml"/>
        <property name="shared" value="true" />
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>
</beans>
  1. 在需要缓存的类或方法前面添加对应注解
    Spring Cache相关注解:
    http://blog.csdn.net/wangshfa/article/details/50678534
    http://www.cnblogs.com/jianjianyang/p/4933016.html

Mybatis-Ehcache

  1. pom.xml,添加依赖包
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.2</version>
        </dependency>
  1. spring-mybatis.xml,添加<setting name="cacheEnabled" value="true" />设置
    <!-- spring和MyBatis整合,不需要mybatis的单独配置文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapper.xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            cacheEnabled=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
  1. XxxMapper.xml,在SQL映射文件前面配置Ehcache设置
<?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="cn.***.dao.XxxMapper" >
   <!--
         以下两个 <cache> 标签二选一,第一个可以输出日志,第二个不输出日志 。
         <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
         <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 
    -->
     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
    
    <!-- 后面省略 -->
</mapper>

在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql,默认情况是true,即该sql使用二级缓存。

<select id="findOrderListResultMap" resultMap="ordersUserMap" useCache="false">

posted on 2017-07-19 13:15  0Kelvin  阅读(216)  评论(0编辑  收藏  举报

导航