mybatis 与 ehcache 整合[转]
1.简介
2. 准备工作
3. 配置步骤:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
create table category ( catid varchar(10) not null, name varchar(80) null, descn varchar(255) null, constraint pk_category primary key (catid));create table product ( productid varchar(10) not null, category varchar(10) not null, name varchar(80) null, descn varchar(255) null, constraint pk_product primary key (productid), constraint fk_product_1 foreign key (category) references category (catid));create index productCat on product (category);create index productName on product (name); |
|
1
2
3
4
|
INSERT INTO category VALUES ('FISH','Fish','<image src="../images/fish_icon.gif"><font size="5" color="blue"> Fish</font>');INSERT INTO category VALUES ('DOGS','Dogs','<image src="../images/dogs_icon.gif"><font size="5" color="blue"> Dogs</font>');INSERT INTO product VALUES ('FI-SW-01','FISH','Angelfish','<image src="../images/fish1.gif">Salt Water fish from Australia');INSERT INTO product VALUES ('FI-SW-02','FISH','Tiger Shark','<image src="../images/fish4.gif">Salt Water fish from Australia'); |
4. cache配置步骤:
|
1
2
3
4
5
6
|
<?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="D:/cache" /> </ehcache> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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不太合适)--> |
|
1
|
<select id="getCategory" parameterType="string" resultType="Category" useCache="false"> |
5. 测试:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
private ApplicationContext application;private CategoryMapper categoryMapper;private ProductMapper productMapper;private String[] categoryId = { "FISH", "DOGS", "REPTILES", "CATS", "BIRDS" };@Beforepublic void initSpring() { application = new FileSystemXmlApplicationContext( "resource/applicationContext.xml"); categoryMapper = application.getBean(CategoryMapper.class); productMapper = application.getBean(ProductMapper.class);}@Testpublic void testSelect() { // the first time long begin = System.nanoTime(); categoryMapper.getCategory(categoryId[0]); long end = System.nanoTime() - begin; print("count :" + end); // the second time begin = System.nanoTime(); categoryMapper.getCategory(categoryId[0]); end = System.nanoTime() - begin; print("count :" + end); // the third time begin = System.nanoTime(); categoryMapper.getCategory(categoryId[0]); end = System.nanoTime() - begin; print("count :" + end); //}@Testpublic void testInsert() { // the second time long begin = System.nanoTime(); // Product p1 = productMapper.getProduct("FI-SW-01"); long end = System.nanoTime() - begin; print("count :" + end); print("Category :"+p1.getCategoryId()); Map<String, String> parame = new HashMap<String, String>(); parame.put("categoryId", "DOGS"); parame.put("productId", "FI-SW-01"); begin = System.nanoTime(); productMapper.updateProductById(parame); end = System.nanoTime() - begin; print("count :"+end); begin = System.nanoTime(); Product p2 = productMapper.getProduct("FI-SW-01"); end = System.nanoTime() - begin; print("count :"+end); print("Category :"+p2.getCategoryId()); } |
count :772080
Category :DOGS
6.总结思考
< diskStore path = "java.io.tmpdir" />
< defaultCache
maxElementsInMemory = "10000"
maxElementsOnDisk = "0"
eternal = "true"
overflowToDisk = "true"
diskPersistent = "false"
timeToIdleSeconds = "0"
timeToLiveSeconds = "0"
diskSpoolBufferSizeMB = "50"
diskExpiryThreadIntervalSeconds = "120"
memoryStoreEvictionPolicy = "LFU"
/>
< cache name = "myCache"
maxElementsInMemory = "100"
maxElementsOnDisk = "0"
eternal = "false"
overflowToDisk = "false"
diskPersistent = "false"
timeToIdleSeconds = "120"
timeToLiveSeconds = "120"
diskSpoolBufferSizeMB = "50"
diskExpiryThreadIntervalSeconds = "120"
memoryStoreEvictionPolicy = "FIFO"
/>
</ ehcache >
diskStore :指定数据存储位置,可指定磁盘中的文件夹位置
defaultCache : 默认的管理策略
- name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
- maxElementsInMemory: 在内存中缓存的element的最大数目。
- maxElementsOnDisk: 在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
- eternal: 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
- timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
- timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
- diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
- diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
- diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
- memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
FIFO ,first in first out (先进先出).
LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
mybatis 与 ehcache 整合[转]的更多相关文章
- 八 mybatis查询缓存(一级缓存,二级缓存)和ehcache整合
1 查询缓存 1.1 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存.
- Mybatis集成ehcache
Mybatis集成ehcache 1.为什么需要缓存 拉高程序的性能 2. 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 3. ehc ...
- mybatis0210 mybatis和ehcache缓存框架整合
.1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...
- Mybatis第八篇【一级缓存、二级缓存、与ehcache整合】
Mybatis缓存 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. myba ...
- mybatis二级缓存应用及与ehcache整合
mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存. 1.开启mybatis的二级缓存 在核心配 ...
- MyBatis - 6.Spring整合MyBatis
1.查看不同MyBatis版本整合Spring时使用的适配包: http://www.mybatis.org/spring/ 2.下载整合适配包 https://github.com/mybatis/ ...
- MyBatis笔记——EhCache二级缓存
介绍 ehcache是一个分布式缓存框架. 我们系统为了提高系统并发,性能.一般对系统进行分布式部署(集群部署方式) 不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统开发.所以要使用分布式缓 ...
- Mybatis 和Spring整合之mapper代理开发
F:\1ziliao\mybatis\代码 1.1 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...
- (十四)mybatis 和 spring 整合
目录 整合思想 整合步骤 整合之后原始 dao 开发 整合之后 Mapper 代理开发 总结 整合思想 让 spring 管理 sqlSessionFactory ,使用 单例模式 创建该对象 : 根 ...
随机推荐
- IE下空链接失效原因及解决方法
我们把a链接的display设置为block,但如果对该标签设置为position:absolute后,会发现在ie6.ie7下有时点击无效,ie8下有效(ie8标准),使用zoom:1方式也无法解决 ...
- 矩形嵌套问题-ACM集训
参考 http://blog.csdn.net/xujinsmile/article/details/7861412 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形 ...
- JS动画理论
动画(Animation) 动画意味着随着时间而变化,尤其指视觉上的变化,包括位置.形态等的变化.运动基本上表现为物体随时间,发生位置上的变化:形态基本表现为大小.颜色.透明度.形状等随时间的变化. ...
- Ruby自学笔记(六)— 循环
循环结构在编程语言中是不可或缺的,所以Ruby中的循环也有其自定义的规则. 而我们关注循环结构,要知道两个因素:1) 循环的条件:2) 循环执行的内容 Ruby有一些方式来实现循环结构体: 1. ti ...
- Java线程生命模型
一. 线程状态类型1. 新建状态(New):新创建了一个线程对象.2. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运 ...
- BZOJ 2432 兔农
Description 农夫栋栋近年收入不景气,正在他发愁如何能多赚点钱时,他听到隔壁的小朋友在讨论兔子繁殖的问题. 问题是这样的:第一个月初有一对刚出生的小兔子,经过两个月长大后,这对兔子从第三个月 ...
- BZOJ 1018 堵塞的交通
Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一 ...
- [BestCoder Round#26] Apple 【组合数学】
题目链接:HDOJ - 5160 题目分析 第一眼看上去,要求统计所有不同排列对答案的贡献.嗯...完全没有想法. 但是,如果我们对每个数字单独考虑,计算这个数字在总答案中的贡献,就容易多了. 对于一 ...
- ubuntu進入dos界面命令 ubuntu進入圖形界面命令
切换界面: ctrl + alt + F1是切到终端模式 Alt+F7 切到图形界面
- 有趣的TWinControl.RecreateWnd,并分析在哪些场合使用
CM_RECREATEWND = CM_BASE + 51; // TWinControl里有对应函数procedure CMRecreateWnd(var Message: TMessage); m ...