Ibatis Study Notes (4) cache 1. cache Configuration
First, let's take a look at the cache configuration style.
<cacheModel id="cache1" type="LRU" readOnly="true" serialize="false"> <property name="cache-size" value="1000" /> </cacheModel> <select id="getProduct" parameterClass="java.lang.Integer"resultClass="product" cacheModel="cache1"><![CDATA[select * from t_product where id = #value#]]></select>
Cachemodel mainly has the following configuration points:
Flushinterval:
Sets the cache validity period. If this value is exceeded, the cache of this cachemodel is cleared.
Size:
The maximum number of data objects in this cachemodel.
Flushonexecute:
Specifies that the cache is cleared when a specific statement is executed. For example, the updateuser operation will change
The user information in the new database, which will lead to the Data Objects in the cache and the actual
Data is deviated, so the cache must be cleared to avoid dirty data.
Type:
Type is divided:
1. Memory
2. LRU
3. FIFO
4. Oscache
Readonly:
The readonly value indicates whether the data objects in the cache are read-only. Here, read-only does not mean that data object 1
Once it is put into the cache, data cannot be modified. But when the data object changes, such
This data object will be abolished from the cache when a property of the object changes.
The database reads data and constructs new data objects.
Readonly = "false" means that the data objects in the cache can be updated, such as the name of the user object.
The attribute is changed.
Serialize:
If global data cache is required, the serialize attribute of cachemodel must be set to true. Otherwise, the data cache is only valid for the current session (which can be simply understood as the current thread), and the local cache can improve the system's integrity.
Limited.
When serialize = "true", if multiple sessions read a data object from the cache at the same time, the cache returns a copy of the object for each session, that is, each session obtains different object instances with the same information. Therefore, the session can access the data obtained from the cache without worrying about synchronization conflicts in the case of multi-thread concurrency.
2. Test
We call the following method:
Product prod = (Product)sqlMap.queryForObject("getProduct", 1);prod = (Product)sqlMap.queryForObject("getProduct", 1);
Then we can check the test result. If the cache is not available, the result is as follows:
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Created connection 324473687.] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100000} Connection] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} PreparedStatement: select * from t_product where id = ? ] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Parameters: [1]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Types: [java.lang.Integer]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} ResultSet] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Header: [id, name, description, url, price]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Result: [1, ?? T400 ???, null, null, 8999.0]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 324473687 to pool.] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Checked out connection 324473687 from pool.] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100003} Connection] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} PreparedStatement: select * from t_product where id = ? ] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} Parameters: [1]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} Types: [java.lang.Integer]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} ResultSet] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} Header: [id, name, description, url, price]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} Result: [1, ?? T400 ???, null, null, 8999.0]] |[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 324473687 to pool.] |
The SQL statements on both sides are executed,
If the cache is configured, it will only be executed once.
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Created connection 423369020.] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100000} Connection] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} PreparedStatement: select * from t_product where id = ? ] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Parameters: [1]] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Types: [java.lang.Integer]] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} ResultSet] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Header: [id, name, description, url, price]] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Result: [1, ?? T400 ???, null, null, 8999.0]] |[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 423369020 to pool.] |