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的更多相关文章

  1. mybatis 3.x 缓存Cache的使用

    mybatis 3.x 已经支持cache功能了,使用很简单,在mappper的xml文件里添加以下节点: <mapper namespace="com.cnblogs.yjmyzz. ...

  2. MyBatis使用DEMO及cache的使用心得

    下面是一个简单的MyBatis使用DEMO. 整体结构 整体代码大致如下: POM依赖 需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工 ...

  3. MyBatis源码分析(4)—— Cache构建以及应用

    @(MyBatis)[Cache] MyBatis源码分析--Cache构建以及应用 SqlSession使用缓存流程 如果开启了二级缓存,而Executor会使用CachingExecutor来装饰 ...

  4. MyBatis源码分析(3)—— Cache接口以及实现

    @(MyBatis)[Cache] MyBatis源码分析--Cache接口以及实现 Cache接口 MyBatis中的Cache以SPI实现,给需要集成其它Cache或者自定义Cache提供了接口. ...

  5. mybatis 缓存(cache)的使用

    许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...

  6. [原创]mybatis详解说明

    mybatis详解 2017-01-05MyBatis之代理开发模式1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只 ...

  7. Spring,Mybatis 整合Memcache

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  8. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  9. MyBatis之代理开发模式

    1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只需要定义dao接口,由mybatis产生dao接口的实现类. 1.1 ...

随机推荐

  1. 转载C#中堆(heap)和栈(stack)的区别

    转载原地址  http://www.cnblogs.com/wangshenhe/archive/2013/02/18/2916275.html [转]C#堆和栈的区别 理解堆与栈对于理解.NET中的 ...

  2. URAL 2065 Different Sums (找规律)

    题意:构造一个数列,使得它们的区间和的种类最少,其中数列中不同的数的数目不少于k. 析:我们考虑0这个特殊的数字,然后0越多,那么总和种类最少,再就是正负交替,那么增加0的数量. 代码如下: #pra ...

  3. Intel HAXM

    Hardware Accelerated Execution Manager的缩写. intel的硬件加速执行管理器,是一款可以使用英特尔虚拟化技术(VT)加快 Android* 开发速度的硬件辅助虚 ...

  4. main函数是个什么东西

    习惯的main函数有无参和两个参数的版本,那么main函数只能这么写吗? 好奇写了一个bug版本的main,结果是呵呵         #include <iostream>        ...

  5. How to Be Good at Mathematics

    How to Be Good at Mathematics Community Q&A Sometimes, the hardest subject for some people is ma ...

  6. struts validate

    1  login.jsp方式1 <%@ page language="java" import="java.util.*" pageEncoding=&q ...

  7. MDF文件数据恢复

  8. Spring声明式事务的配置~~~

    /*2011年8月28日 10:03:30 by Rush  */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...

  9. C++ 析构方法

    1.什么是析构方法? 析构方法与构造方法互补. 2.为什么设计析构方法? 构造方法创建一个对象,对象内部往往还会申请一些资源.设计析构方法的目的是 释放资源,同时销毁自身. 3.析构方法可以认为分为两 ...

  10. Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学

    B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...