MyBatis(3.2.3) - Cache
Caching data that is loaded from the database is a common requirement for many applications to improve their performance. MyBatis provides in-built support for caching the query results loaded by mapped SELECT statements. By default, the first-level cache is enabled; this means that if you'll invoke the same SELECT statement within the same SqlSession interface, results will be fetched from the cache instead of the database.
We can add global second-level caches by adding the <cache/> element in SQL Mapper XML files.
When you'll add the <cache/> element the following will occur:
- All results from the <select> statements in the mapped statement file will be cached
- All the <insert>, <update>, and <delete> statements in the mapped statement file will flush the cache
- The cache will use a Least Recently Used (LRU) algorithm for eviction
- The cache will not flush on any sort of time-based schedule (no Flush Interval)
- The cache will store 1024 references to lists or objects (whatever the query method returns)
- The cache will be treated as a read/write cache; this means that the objects retrieved will not be shared and can safely be modified by the caller without it interfering with other potential modifications by other callers or threads
You can also customize this behavior by overriding the default attribute values as follows:
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
A description for each of the attributes is as follows:
- eviction: This is the cache eviction policy to be used. The default value is LRU. The possible values are LRU (least recently used), FIFO(first in first out), SOFT(soft reference), WEAK(weak reference).
- flushInterval: This is the cache flush interval in milliseconds. The default is not set. So, no flush interval is used and the cache is only flushed by calls to the statements.
- size: This represents the maximum number of elements that can be held in the cache. The default is 1024, and you can set it to any positive integer.
- readOnly: A read-only cache will return the same instance of the cached object to all the callers. A read-write cache will return a copy (via serialization) of the cached object. The default is false and the possible values are true and false.
- The JavaBeans must implements the interface java.io.Serializable.
A cache configuration and cache instance are bound to the namespace of the SQL Mapper file, so all the statements in the same namespace table as the cache are bound by it.
The default cache configuration for a mapped statement is:
<select ... flushCache="false" useCache="true"/>
<insert ... flushCache="true"/>
<update ... flushCache="true"/>
<delete ... flushCache="true"/>
You can override this default behavior for any specific mapped statements; for example, by not using a cache for a select statement by setting the useCache="false" attribute.
In addition to in-built Cache support, MyBatis provides support for integration with popular third-party Cache libraries, such as Ehcache, OSCache, and Hazelcast.
MyBatis(3.2.3) - Cache的更多相关文章
- mybatis 3.x 缓存Cache的使用
mybatis 3.x 已经支持cache功能了,使用很简单,在mappper的xml文件里添加以下节点: <mapper namespace="com.cnblogs.yjmyzz. ...
- MyBatis使用DEMO及cache的使用心得
下面是一个简单的MyBatis使用DEMO. 整体结构 整体代码大致如下: POM依赖 需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工 ...
- MyBatis源码分析(4)—— Cache构建以及应用
@(MyBatis)[Cache] MyBatis源码分析--Cache构建以及应用 SqlSession使用缓存流程 如果开启了二级缓存,而Executor会使用CachingExecutor来装饰 ...
- MyBatis源码分析(3)—— Cache接口以及实现
@(MyBatis)[Cache] MyBatis源码分析--Cache接口以及实现 Cache接口 MyBatis中的Cache以SPI实现,给需要集成其它Cache或者自定义Cache提供了接口. ...
- mybatis 缓存(cache)的使用
许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...
- [原创]mybatis详解说明
mybatis详解 2017-01-05MyBatis之代理开发模式1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只 ...
- Spring,Mybatis 整合Memcache
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- (转)springMVC+mybatis+ehcache详细配置
一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...
- MyBatis之代理开发模式
1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只需要定义dao接口,由mybatis产生dao接口的实现类. 1.1 ...
随机推荐
- oracle 的rowid和rownum
rowid就是唯一标志记录物理位置的一个id,对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行 ...
- HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
- Tiny6410声卡驱动——录音与回放
在Linux下,音频设备程序的实现与文件系统的操作密切相关.Linux将各种设备以文件的形式给出统一的接口,这样的设计使得对设备的编程与对文件的操作基本相同,对Linux内核的系统调用也基本一致,从而 ...
- java 数据库连接池 Oracle版
首先应加入连接池和数据库连接的配置文件:数据库连接包:ojdbc6.jar数据库连接池包:commons-pool2-2.2.jar commons-dbc ...
- Cocos2d-x——CocosBuilder官方帮助文档翻译2 多分辨率支持
Working with Multiple Resolutions 多分辨率设置 A common scenario when creating apps or games is to target ...
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
- JAVA反射机制学�
JAVA反射机制:对于随意一个类,都可以知道这个类的全部属性和方法:对于随意一个对象,都可以调用它的随意一个方法和属性:这样的动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. J ...
- cisco路由器配置教程
配置cisco路由器 经过几十年的发展,从最初的只有四个节点的ARPANET发展到现今无处不在的Internet,计算机网络已经深入到了我们生活当中.随着计算机网络规模的爆炸性增长,作为连接设备的路由 ...
- Windows下用Git下载android源码 转载
http://my.oschina.net/jiadebin/blog/52631 1.首先你的电脑要安装好git,这个请参考git官网. 2.打开git命令窗口输入git clone http:// ...
- oracle procedure存储过程(pl/sql)_使用declare cursor_begin end嵌套
create or replace procedure PRO_DelArticles ( ArticleId in varchar2 ) is ArticleNum varchar2(20); sq ...