MyBatis contains a very powerful query caching feature that can be easily configured and customized. By default, the cache is not turned on, and to turn on level two caching, you need to add a line to your SQL map file:
This is literally the case. 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 refresh the cache.
3. The cache is retracted using least recently Used (LRU, least recently used) algorithm.
4. According to the schedule (e.g. no Flush Interval, no refresh interval), the cache is not refreshed in any chronological order.
5. Caching stores the 1024 references to a list collection or object, regardless of what the Query method returns.
6. Caching is 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 caching the attributes of the element. Like what:
<cache
eviction= "FIFO"
flushinterval= "60000" size= "" "readonly="
true "/>
This more advanced configuration creates a FIFO cache, refreshes every 60 seconds, stores the 512 references to the result object or list, and the returned objects are considered read-only, so modifying them between callers in different threads can cause conflicts.
The available recovery policies (eviction) are:
- lru– least Recently used: remove objects that are not used for the longest time.
- fifo–: Remove them by the order in which the objects enter the cache.
- soft– Soft Reference: Removes objects that are based on garbage collector state and soft reference rules.
- weak– Weak reference: More aggressively removes objects based on garbage collector state and weak reference rules.
The default is LRU.
The Flushinterval (refresh interval) can be set to any positive integer, and they represent a reasonable millisecond-form time period. The default is not set, that is, there is no refresh interval, and the cache refreshes only when the statement is invoked.
The size (number of references) can be set to any positive integer, remembering the number of objects you cache and the amount of available memory resources in your running environment. The default value is 1024.
The readOnly (read-only) 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 a very important performance advantage. A writable cache returns a copy of the cached object (via serialization). This is slower, but safe, so the default is false.
Using Custom Caching
In addition to these custom caching methods, you can also completely overwrite the caching behavior by implementing your own cache or creating adapters for other third-party caching schemes.
<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 simply given 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 will want to share the same cache configuration and instance in the namespace. In such cases you can use the Cache-ref element to refer to another cache.