MyBatisEhcache二级缓存的开启

mybatis二级缓存对细粒度的数据级别的缓存实现不好,比如如下需求:对商品信息进行缓存,由于商品信息查询访问量大,但是要求用户每次都能查询最新的商品信息,
           此时如果使用mybatis的二级缓存就无法实现当一个商品变化时只刷新该商品的缓存信息而不刷新其它商品的信息,因为mybaits的二级缓存区域以mapper为单位划分,
           当一个商品信息变化会将所有商品信息的缓存数据全部清空。解决此类问题需要在业务层根据需求对数据有针对性缓存。
      缓存都是实现了Cache这个接口.....
 

如何开启 二级缓存,步骤如下:
        1.导入ehcache相关jar包  (ehcache: 缓存插件,插件:就是对现有应用软件功能的一个扩展)        

      ehcache-core-2.6.5.jar
        mybatis-ehcache-1.1.0.jar
 
      <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache-core</artifactId>
          <version>2.6.6</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
      <dependency>
          <groupId>org.mybatis.caches</groupId>
          <artifactId>mybatis-ehcache</artifactId>
          <version>1.1.0</version>
      </dependency>

   2,开启mybatis的二级缓存 

在mybatis核心配置文件mybatis-config.xml中加入
 
            <settings>
                <!-- 开启二级缓存 -->
                <setting name="cacheEnabled" value="true" />
            </settings>
<?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.mybatis.dao.EmployeeMapper">
	<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
</mapper>

 

3.在classpath下加入ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 
    <diskStore path="java.io.tmpdir"/>
 
	<defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
			
	<!--
		默认缓存配置,
		以下属性是必须的:
			name :cache的标识符,在一个CacheManager中必须唯一。
			maxElementsInMemory : 在内存中缓存的element的最大数目。
			maxElementsOnDisk : 在磁盘上缓存的element的最大数目。
			eternal : 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略。
			overflowToDisk : 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上。
		以下属性是可选的:
			 timeToIdleSeconds : 缓存element在过期前的空闲时间。
			 timeToLiveSeconds : 缓存element的有效生命期。
			 diskPersistent : 在VM重启的时候是否持久化磁盘缓存,默认是false。
			 diskExpiryThreadIntervalSeconds : 磁盘缓存的清理线程运行间隔,默认是120秒.
			 memoryStoreEvictionPolicy : 当内存缓存达到最大,有新的element加入的时候,
				移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO
	-->
</ehcache>

测试整合ehcache的二级缓存

SqlSession openSession = sqlSessionFactory.openSession();
SqlSession openSession2 = sqlSessionFactory.openSession();

try {
	DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
	DepartmentMapper mapper2 = openSession2.getMapper(DepartmentMapper.class);
	
	Department departmentById = mapper.getDepartmentById(1);
	System.out.println(departmentById);
	openSession.close();
	Department departmentById2 = mapper2.getDepartmentById(1);
	System.out.println(departmentById2);
	
} catch (Exception e) {
	e.printStackTrace();
}finally {
	
	openSession2.close();
}

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值