MyBatis contains a very powerful query cache feature that can be configured and customized very easily. By default, cache is not enabled. To enable level 2 cache, you need to add a line to your SQL mapping file:
<cache/>
That's it literally. The effect of this simple statement is as follows:
1. All select statements in the mapping statement file will be cached.
2. Map all insert, update and delete statements in the statement file will refresh the cache.
3. The cache will be retried using the Least Recently Used (LRU, the least recently used) algorithm.
4. According to the timetable (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any chronological order.
5. The cache stores 1024 references to a list collection or object (regardless of what the query method returns).
6. The cache will be considered a read/write (readable/writable) cache, meaning that object retrieval is not shared and can be safely modified by the caller without interfering with potential modifications made by other callers or threads.
All of these properties can be modified by cache elements' properties. for example:
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
This more advanced configuration creates a FIFO cache and refreshes every 60 seconds, storing 512 references to the result object or list, and the returned object is considered read-only, so modifying them between callers in different threads results in conflicts.
The available retrieval strategies are:
The default is LRU.
flushInterval can be set to any positive integer, and they represent a reasonable milliseconds time period. The default is not set, that is, there is no refresh interval, and the cache is refreshed only when the statement is called.
size (number of references) can be set to any positive integer, remembering the number of objects you cache and the number of available memory resources in your running environment. The default value is 1024.
The readOnly property can be set to true or false. A read-only cache returns the same instance of the cache object to all callers. Therefore, these objects cannot be modified. This provides important performance advantages. A readable and writeable cache returns a copy of the cache object (by serialization). This will be slower, but safe, so it is false by default.
Using custom cache
In addition to these custom cache methods, you can also completely override cache behavior by implementing your own cache or creating adapters for other third-party cache scenarios.
<cache type=”com.domain.something.MyCustomCache”/>
This example shows how to use a custom cache implementation. The class specified by the type attribute must implement the org.mybatis.cache.Cache interface. This interface is one of many complex interfaces in the MyBatis framework, but it is fine to simply give it what it does.
public interface Cache { String getId(); int getSize(); void putObject(Object key, Object value); Object getObject(Object key); boolean hasKey(Object key); Object removeObject(Object key); void clear(); ReadWriteLock getReadWriteLock(); } Reference cache
Maybe sometime in the future, you'll want to share the same cache configuration and instances in the namespace. In this case you can use the cache-ref element to reference another cache.
<cache-ref namespace=”com.someone.application.data.SomeMapper”/>