MyBatis (4) _ Level Two cache deep _ use third-party Ehcache to configure two-level caching

Source: Internet
Author: User

Effect of adding and deleting to level two cache

1. Add or remove changes will also empty the level two cache 2. For the two-level cache, emptying is essentially a null,key of value, not deleting the entry<k,v> 3. The condition for a select query from DB is: 1. This key 2 does not exist in the cache at all. There is a key corresponding to the entry, but value is null

Configuration of the second-level cache

<cache eviction= "FIFO"

Flushinterval= "60000"

Size= "readonly=" "true"/>

This more advanced configuration creates a FIFO cache, refreshes every 60 seconds, saves 512 references to the resulting object or list, and the returned object is considered read-only

Available Retract policies

lru– Least Recently used: Removes objects that are not used for the longest time. fifo– FIFO: Removes them by the order in which they are entered in the cache. soft– Soft Reference: Removes objects based on the garbage collector state and soft reference rules. weak– Weak references: More aggressively remove objects based on the 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 in the form of a time period. The default is not set, that is, there is no refresh interval, and the cache simply refreshes when the statement is invoked. The size (number of references) can be set to any positive integer, keeping in mind the number of objects you cache and the number of available memory resources for the environment you are running. 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 read-write cache returns a copy of the cached object (by serialization). This will be slower, but safe, so the default is false.

turn on Local level two cache: Add flushcache= "true" to the tag

<id= "Findstudent"  resulttype= "Student"  Flushcache = "true" >         SELECT * FROM student    </Select>

Configuring level Two caching with third-party Ehcache

1> introduction of Mybatis-ehcache-1.0.3.jar and Ehcache-core-2.6.11.jar

2> add Ehcache.xml under src You can find the location in the jar package as follows

3> Setting the cache reference to Ehcache in mapper

<type= "Org.mybatis.caches.ehcache.EhcacheCache"/>

MyBatis (4) _ Level Two cache deep _ use third-party Ehcache to configure two-level caching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.