SSM Framework consolidates Ehcache cache

Source: Internet
Author: User

First, the introduction of MAVEN

        <!--Ehcache Core Jar Package -        <Dependency>            <groupId>Net.sf.ehcache</groupId>            <Artifactid>Ehcache-core</Artifactid>            <version>2.6.11</version>        </Dependency>        <!--MyBatis and Ehcache integrated jar package -        <Dependency>            <groupId>Org.mybatis.caches</groupId>            <Artifactid>Mybatis-ehcache</Artifactid>            <version>1.1.0</version>        </Dependency>        <Dependency>            <groupId>Org.slf4j</groupId>            <Artifactid>Slf4j-log4j12</Artifactid>            <version>1.6.4</version>        </Dependency>    

Second, add configuration file under Classpath path Ehcache.xml

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Ehcache.xsd">    <DiskstorePath= "F:\develop\ehcache" />    <Defaultcachemaxelementsinmemory= "+"Maxelementsondisk= "10000000"Eternal= "false"Overflowtodisk= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU">    </Defaultcache></Ehcache>

Attention:

Xsi:nonamespaceschemalocation= "Ehcache.xsd" This is where I download the constraint file (http://ehcache.org/ehcache.xsd) on the network to the local path for reading

Because his original statement in idea reported wrong, for the trouble, I directly cache it, the round file as follows

  

<xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"  xsi: noNamespaceSchemaLocation= ". /config/ehcache.xsd ">

Defaultcache Property Configuration Detailed

Diskstore: Specifies where the data is stored on disk. Defaultcache: When you create a cache with Cachemanager.add ("Democache"), the Ehcache takes<Defalutcache/>The following properties of the specified management policy are required: maxelementsinmemory-The maximum number of element caches in memory Maxelementsondisk-the maximum number of element that is cached on disk, or 0 for infinity Eternal- Sets whether the cached elements will never expire. If true, the cached data is always valid, and if False then the Overflowtodisk is judged according to Timetoidleseconds,timetoliveseconds- Sets whether the expired element is cached on disk when the memory cache overflows the following attributes are optional: timetoidleseconds-When the cached data in Ehcache is two times longer than the Timetoidleseconds attribute value, The data is deleted, and the default value is 0, which is the maximum idle time timetoliveseconds-the valid lifetime of the cache element, which is 0 by default. This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer. Diskpersistent-whether to enable the disk to save the data in Ehcache when the VM restarts, false by default. Diskexpirythreadintervalseconds-Disk cache cleanup thread run interval, default is 120 seconds. Each 120s, the corresponding thread will perform a cleanup of the data in Ehcache Memorystoreevictionpolicy-When the memory cache is maximized and a new element is added, the policy of element in the cache is removed. The default is LRU (least recently used), optional lfu (most infrequently used) and FIFO (first-in-one-out)

Note: The survival time problem of Ehcache


If the existing timetoliveseconds also have timetoidleseconds then take the minimum count as the interval; min (x, y);;
After testing the principle of calculation is: if the cache has not been accessed since the creation of the cache, then the interval x after the failure, if the cache since the creation of n access to the cache, then the calculation (last access cache time +y)
That is: According to timetoidleseconds calculation, but the total survival time does not exceed Y;
An example: timetoidleseconds=120;
timetoliveseconds=180;

The above indicates that this cache can live for up to 3 minutes, and this cache fails if the period exceeds 2 minutes of access.

Third, open the Ehcache cache

Modify ... mapper.xml, add the specified Ehcachecache to the cache

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

or custom cache

<Mappernamespace= "Com.ruoweiedu.api.app.querypricelist.xml.QueryPriceList">    <Cachetype= "Org.mybatis.caches.ehcache.EhcacheCache" >        < Propertyname= "Timetoidleseconds"value= "+"/>        < Propertyname= "Timetoliveseconds"value= "+"/>        <!--same Ehcache parameter maxelementsinmemory -        < Propertyname= "Maxentrieslocalheap"value= "+"/>        <!--same Ehcache parameter Maxelementsondisk -        < Propertyname= "Maxentrieslocaldisk"value= "10000000"/>        < Propertyname= "Memorystoreevictionpolicy"value= "LRU"/>    </Cache>

Iv. Introduction of application environment

For access to many query requests and users of the query results real-time requirements are not high, at this time can be used MyBatis two cache technology to reduce database access, improve access speed, business scenarios such as:

Time-consuming statistical analysis of SQL, phone Bill query SQL, and so on. The implementation is as follows: By setting the refresh interval time, the cache is automatically emptied by mybatis at intervals, and the cache refresh interval is set according to the frequency of data change

Flushinterval, such as set to 30 minutes, 60 minutes, 24 hours, etc., depending on the needs.

MyBatis level Two cache for fine-grained data-level cache implementation is not good, such as the following requirements: The product information cache, because the product information query access is large, but require users every time can query the latest product information,

At this point, if you use MyBatis's level two cache, you will not be able to realize that when a commodity changes, only the cache information of the product is refreshed, not the information of other goods, because Mybaits's level two cache area is divided in Mapper, when a

A change in the product information will erase all the cached data for all commodity information. Addressing such issues requires a targeted cache of data at the business level based on demand.

  

SSM Framework consolidates Ehcache cache

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.