青岛做网站企业排名,如何在云服务器上开多几个网站,wordpress 显示小工具栏,大数据做网站1、前言 前面讲解了mybatis的一级、二级缓存。一级然并卵#xff08;spring整合后#xff09;#xff0c;二级还是有用的。我们现在来看看用ehcache来维护管理二级缓存。不要问我为什么#xff0c;因为都这么用#xff01;#xff01;#xff01;java是框架语言#x… 1、前言 前面讲解了mybatis的一级、二级缓存。一级然并卵spring整合后二级还是有用的。我们现在来看看用ehcache来维护管理二级缓存。不要问我为什么因为都这么用java是框架语言人家给你买个了车车你硬是要自己写个赛跑的车你认为呢(精神可嘉) 2、配置ehcache 我们要先有个态度前面我们已经有了mybatis的缓存的设置知道其实质就是用map把数据存起来这TM就是缓存。所以这些第三方框架也就是做了同样的事情因为他们更专业。 2.1配置ehcache.xml 把文件放置到resource下面 span stylefont-size:10px;ehcache xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocationhttp://ehcache.org/ehcache.xsd!--diskStore缓存数据持久化的目录 地址 --diskStore pathF:\ycydevelop\ehcache /defaultCache maxElementsInMemory1000 maxElementsOnDisk10000000eternalfalse overflowToDiskfalse diskPersistenttruetimeToIdleSeconds120timeToLiveSeconds120 diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRU/defaultCache
/ehcache/span 2.2 配置具体的mapper到具体的mapper.xml就这样简单就这么任性。我加入的是userMapper.xml。 !--打开mapper二级缓存开关--cache typeorg.mybatis.caches.ehcache.EhcacheCache/ 如果你希望你本地mapper与全局的sqlconfig不一样的的时候你已经可以设置缓存 !--打开mapper二级缓存开关--cache typeorg.mybatis.caches.ehcache.EhcacheCacheproperty namemaxElementsInMemory value1000//cache 2.3 测试二级缓存 依旧用以前测试 开启两个sqlsession package com.ycy.mybatis.test;import com.ycy.mybatis.dao.OrdersCustomMapper;
import com.ycy.mybatis.dao.UserMapper;
import com.ycy.mybatis.dao.impl.UserMappermpl;
import com.ycy.mybatis.module.Orders;
import com.ycy.mybatis.module.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** Created by Administrator on 2015/8/31 0031.*/
public class MybatisTest9 {private SqlSessionFactory sqlSessionFactory null;Beforepublic void before() throws IOException {String resourceSqlMapConfig.xml;InputStream in Resources.getResourceAsStream(resource);sqlSessionFactory new SqlSessionFactoryBuilder().build(in);}//一级缓存测试Testpublic void findOrderAndDetail() throws Exception {SqlSession sqlSessionsqlSessionFactory.openSession();//第一次查询UserMapper userMappersqlSession.getMapper(UserMapper.class);User user userMapper.getUserById(1);System.out.println(user.getUsername());//第二次查询没有关闭sqlsessionUser user2 userMapper.getUserById(1);System.out.println(user2.getUsername());}//二级缓存测试Testpublic void cache2() throws Exception {SqlSession sqlSessionsqlSessionFactory.openSession();SqlSession sqlSession2sqlSessionFactory.openSession();UserMapper userMappersqlSession.getMapper(UserMapper.class);UserMapper userMapper2sqlSession2.getMapper(UserMapper.class);//第一次查询User user userMapper.getUserById(1);System.out.println(user.getUsername());sqlSession.close();//第二次查询User user2 userMapper2.getUserById(1);System.out.println(user2.getUsername());sqlSession2.close();}}测试二级缓存结果看到0.5了吧亲爱的小伙伴 name:缓存名称。 maxElementsInMemory缓存最大个数。 eternal:对象是否永久有效一但设置了timeout将不起作用。 timeToIdleSeconds设置对象在失效前的允许闲置时间单位秒。仅当eternalfalse对象不是永久有效时使用可选属性默认值是0也就是可闲置时间无穷大。 timeToLiveSeconds设置对象在失效前允许存活时间单位秒。最大时间介于创建时间和失效时间之间。仅当eternalfalse对象不是永久有效时使用默认是0.也就是对象存活时间无穷大。 overflowToDisk当内存中对象数量达到maxElementsInMemory时Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB这个参数设置DiskStore磁盘缓存的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk硬盘最大缓存个数。 diskPersistent是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds磁盘失效线程运行时间间隔默认是120秒。 memoryStoreEvictionPolicy当达到maxElementsInMemory限制时Ehcache将会根据指定的策略去清理内存。默认策略是LRU最近最少使用。你可以设置为FIFO先进先出或是LFU较少使用。 clearOnFlush内存数量最大时是否清除。 2、二级缓存应用场景 适用性对查询频率高变化频率低的数据建议使用二级缓存。例如我们的账单信息查询 局限性mybatis二级缓存对细粒度的数据级别的缓存实现不好例如我们的商品信息广告信息实时在更新。而二级缓存是针对mapper的如果order里面更新 了那么里面关于order的缓存就清空了哦。