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.