mybatis record casually (six) cache mechanism

First, the cache

MyBatis exists in the cache SqlSession life cycle , when the same query in a SqlSession, MyBatis will the methods and parameters of execution generated by the cache key algorithm, and the key query results into a Map object. If the same methods and parameters SqlSession performed in exactly the same, then the algorithm will be generated by the same key, when the key is present in the object cache already Map, will return the object cache.

SysUser user1 = userMapper.selectByid(lL);

user1.setUserName("New Name");

SysUser user2 = userMapper.selectByid(lL);

Currently only perform a database query, if the re-assignment of user1, user2 corresponding to the query object value also changes the current query for a username user2 "New Name".

If you want to remove a cache (not recommended), you can set the attribute FL ushCache = " to true", as follows

[Note] any INSERT, UPDATE, DELETE operations will empty the cache, performed in INSERT, UPDATE, DELETE method after inquiry will re-query the database.

 

Second, the secondary cache

MyBatis secondary cache is very powerful, it is different from a cache exists only in SqlSession life cycle, but can be understood to be present in SqlSessionFactory life cycle.

Secondary cache configuration

There is a parameter in cacheEnabled MyBatis global configuration settings, this parameter is global cache two switches, the default value is true, the initial state is enabled. If this parameter is set to false, even if the back of the secondary cache configuration, it will not take effect.

[Description] secondary cache that is enabled by default, not the configuration.

And a secondary cache MyBatis namespace binding, i.e., arranged in the secondary cache needs Mapper. Xml file mapping in or arranged in Mapper.java interface. In the mapping file, namespace is the XML namespace attribute of the root node mapper. In Mapper interface, a namespace is the fully qualified name of the interface.

2.1 Mapper.xml Configuration

Configuring <cache /> to, the following:

The default secondary cache will have the following effects:

  • All SELECT statements in the mapped statement file will be cached.
  • All INSERT statements mapping file, UPDATE, DELETE statements will flush the cache.
  • The cache will use the Least Recently Used (LRU, least recently used) algorithm for eviction.
  • The schedule (e.g., no Flush Interval, no refresh interval), the cache will not be any sort of time to refresh.
  • The cache will store collections or objects (no matter what type of query method returns a value of) the 1024 reference.
  • The cache will be treated as read / write (read / write), meaning objects retrieved are not shared and can be safely modified by the caller, the caller without interfering with other potential modifications or threads made.

All of these properties can be modified by the properties of the cache element, the following example.
<Cache
eviction = "the FIFO"
flushlnterval = "60000"
size = "512"
readOnly = "to true" />


Property description:
eviction (eviction policies)

LRU (least recently used): Removes objects that have not been used for a long time, which is the default.

FIFO (First In First Out): the object into the cache in order to remove them.

SOFT (soft references): Removes objects based on the garbage collector state and rules of soft references.

WEAK (weak references): More aggressively removes objects based on the garbage collector state and rules of Weak References.

flushinterval (refresh interval)

It may be set to any positive integer, and should represent a reasonable period of time specified in milliseconds. Default is not set, ie no refresh interval, the cache is only refreshed when the call statement.

size (number of references)

Can be set to any positive integer, keep in mind that the number of available memory resources and the number of objects in the cache of the operating environment. The default value is 1024.

readOnly (Read Only)

Attribute can be set to true or false. Read-only cache will return the same to all callers cached object instance, these objects can not be modified, which offers a significant performance advantage.

Read-write cache will return a copy of the cache object serialization, which may be slower, but the security, the default is false.

 

2.2 Mapper interface configuration

When using the annotation mode , if you want to enable annotation method secondary cache, also you need to configure Mapper interface, if Mapper interface is also there is a corresponding XML mapping file, when both open the cache, also need special configuration.

When only the secondary cache is disposed annotations, add the following configuration in the interface:
@CacheNamespace (

eviction = FifoCache.class,
flushinterval = 60000,
size = 512,
readWrite = true

When the use mode annotations and XML mapping files , the interface and the corresponding Mapper XML namespace file is the same, to the use of the secondary cache, both must be configured the same time, this should be used when referring to the cache, as follows

@CacheNamespaceRef(RoleMapper.class)

public interface RoleMapper{

}
Since Do method RoleMapper interface and the XML annotation methods use the same cache, the cache using the reference configuration so RoleMapper.class, which would use the namespace cache configured com.chl.mapper.RoleMapper, i.e. RoleMapper. xml configured cache.

Mapper XML mapping interface may reference files or other annotations via cache interface, the cache may be arranged in the reference XML, as may be modified as follows in the RoleMapper.xml
< Cache-REF narnespace = "com.chl.mapper.RoleMapper" / >
after this configuration, XML Mapper will quote the secondary cache interface configuration, the same configuration at the same time to avoid conflicts resulting from the secondary cache.

Configuration is as follows:

 

Example 2.3 buffer

Because the use of write cache, the cache may be used SerializedCache serialization. The cache requires that all classes of objects to be serialized must implement Serializable (java.io.Serializable) interface.

Modify SysRole.java

 

Modify RoleMapper.java

修改RoleMapper.xml

通过JUnit测试

 

日志输出

日志中存在好几条以 Cache Hit Ratio 开头的语句 ,这行日志后面输出的值为当前执行方法的缓存命中率。

Guess you like

Origin www.cnblogs.com/lovechengyu/p/11276385.html