Home  >  Article  >  Java  >  Tutorial on using XML mapping cache in Java's MyBatis framework

Tutorial on using XML mapping cache in Java's MyBatis framework

高洛峰
高洛峰Original
2017-01-23 11:41:261300browse

MyBatis includes a very powerful query caching feature that can be configured and customized very easily. By default, caching is not enabled. To enable secondary caching, you need to add a line to your SQL mapping file:

<cache/>

That's literally it. The effect of this simple statement is as follows:
1. All select statements in the mapping statement file will be cached.
2. All insert, update and delete statements in the mapping statement file will refresh the cache.
3. The cache will use the Least Recently Used (LRU, least recently used) algorithm to recover.
4. According to the schedule (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any time order.
5. The cache will store 1024 references to the list collection or object (regardless of what the query method returns).
6. The cache is considered a read/write cache, which means that object retrieval is not shared and can be safely modified by the caller without interfering with potential changes made by other callers or threads. Revise.

All of these properties can be modified through the cache element's properties. for example:

<cache
 eviction="FIFO"
 flushInterval="60000"
 size="512"
 readOnly="true"/>

This more advanced configuration creates a FIFO cache, flushed every 60 seconds, that stores 512 references to the resulting object or list, and the returned object is considered read-only and therefore modified between callers in different threads They can cause conflicts.
Available eviction strategies (eviction) are:

LRU – Least Recently Used: Remove objects that have not been used for the longest time.

FIFO – First in, first out: Remove objects in the order they enter the cache.

SOFT – Soft Reference: Removes objects based on garbage collector status and soft reference rules.

WEAK – Weak References: More aggressively remove objects based on garbage collector state and weak reference rules.

The default is LRU.

flushInterval can be set to any positive integer, and they represent a reasonable period of time in milliseconds. The default is not set, that is, there is no refresh interval, and the cache is only refreshed when the statement is called.
size (number of references) can be set to any positive integer, keeping in mind the number of objects you cache and the amount of memory resources available in your environment. Default value is 1024.
The readOnly property can be set to true or false. A read-only cache returns the same instance of the cached object to all callers. Therefore these objects cannot be modified. This provides important performance advantages. A read-write cache returns a copy of the cached object (via serialization). This is slower, but safer, so defaults to false.

Using custom cache
In addition to these ways of customizing caching, you can also completely override caching behavior by implementing your own cache or creating adapters for other third-party caching solutions.

<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 the many complex interfaces in the MyBatis framework, but 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 at some point in the future you'll want to share the same cache configuration and instances across namespaces. In such cases you can use the cache-ref element to reference another cache.​

<cache-ref namespace=”com.someone.application.data.SomeMapper”/>

For more tutorials on the use of XML mapping cache in Java's MyBatis framework, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn