Springmvc+mybatis+ehcache Detailed Configuration

Source: Internet
Author: User

(1) First need to need two main jar package

Ehcache-core-2.4.6.jar

Mybatis-ehcache-1.0.1.jar Ehcache-core must have more than 1.3 versions since 1.3 didn't seem to support the cluster.

(2) Create a ehcache.xml under the Classpath

<?xml version= "1.0" encoding= "Utf-8"?> <ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation= "Ehcache.xsd" > <diskstore path= "Java.io.tmpdir"/> &LT;DEFAULTCAC He maxelementsinmemory= "10000" eternal= "false" timetoidleseconds= "" timetoliveseconds= "" overflowtodisk= "false" /> <!--Configure Custom caching maxelementsinmemory: The maximum number of objects allowed in the cache eternal: whether the object in the cache is forever          
    Long, if it is, the timeout setting is ignored and the object never expires.                      
                    Timetoidleseconds: The passivation time of cached data, which is the maximum time interval value of two access times before an element dies, which can only be effective if the element is not permanently resident.          
    If the value is 0, it means that the element can pause for an infinite length of time. Timetoliveseconds: The lifetime of the cached data, which is the maximum time interval value for an element from construction to extinction, which can only be valid if the element is not permanently resident, if the value is 0 means the element can pause indefinitely          
    A long time.          
    Overflowtodisk: Disk caching is enabled when there is not enough memory.      
    Memorystoreevictionpolicy: Cache full after the elimination algorithm.   --> <cache name= "Testcache"        
    Maxelementsinmemory= "10000" eternal= "true" overflowtodisk= "false" timetoidleseconds= "0" timetoliveseconds= "memorystoreevictionpolicy=" LFU "/> </e Hcache>

Above the Diskstor path you can specify a path, Java.io.tmpdir refers to your system's cache directory, you can Baidu and then generally this XML needs to have a defaultcache, is the default cache configuration What are the parameters inside you can check the API online

Then I also configured a testcache, I look for information on the internet did not see where to explicitly, and then my own test, found that Ehcache can generate a number of cache, each cache can be based on different business scenarios for different business (that is, the parameters in the configuration of different),    So this seems to be a lot of configuration, in fact, more flexibility. (3) Add a configuration to the spring configuration file:

<!--use Ehcache cache-->    
   <bean id= "Ehcachemanager" class= " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean ">  
     <property name=" configlocation "value=" Classpath:ehcache.xml "/>  
   </bean>
So we can integrate Ehcache and spring.
(4) Add in the corresponding Mapper.xml:
<cache type= "Org.mybatis.caches.ehcache.LoggingEhcache" >  
    <property name= "Timetoidleseconds" 3600 "/><!--1 hour-->  
    <property name=" timetoliveseconds "value=" 3600 "/><!--1 hour-->  
    <property name= "Maxentrieslocalheap" value= "1000"/> <property name= "Maxentrieslocaldisk"  
    10000000 "/> <property name= memorystoreevictionpolicy" value= "LRU"/>  
</cache>

After the parameter configuration can also be, there will be a default value, we can also check what the total configuration, and then according to their own needs to configure, and then this configuration is to take the cache to execute the log, if not with the log can be changed to Ehcachecache Logginehcache.

In Mapper.xml this set the default is all actions will execute the caching policy, if some SQL does not need to execute, you can set the UseCache to False.

<select id= "Selectbyexample" resultmap= "Baseresultmap" Com.anenjoy.manage.entity.TblUserTempExample "Usecache=" False >
In fact, after such a configuration Ehcache has been basically OK. Let's test it next.
The original test code is as follows:
Long begin = System.nanotime ();  
        Tempservice.selectall ();  
        Long end = System.nanotime ()-Begin;  
        System.out.println ("Count:" + end);  
        The second time  
        begin = System.nanotime ();  
        try {  
            tempservice.inserttblusertemp ("1", "1", "1", "1");  
        } catch (NoSuchAlgorithmException e) {  
            E.printstacktrace ();  
        }  
        End = System.nanotime ()-Begin;  
        System.out.println ("Count:" + end);  
        The second time  
        begin = System.nanotime ();  
        Tempservice.selectall ();  
        End = System.nanotime ()-Begin;  
        System.out.println ("Count:" + end);  
        The third time  
        begin = System.nanotime ();  
        Tempservice.selectall ();  
        End = System.nanotime ()-Begin;  
        System.out.println ("Count:" + end);  
        Return "";

There are 4 output statements inside this

First, query SQL for a table at a time

Then insert a message into a table to execute the insert SQL

Then query the table to execute the query SQL, which is correct because you executed the non-query statement, this time need to re-access the database

Then again query a table does not execute query SQL This means that the cache is useful, he did not access the database, directly from the memory or disk to obtain

You can also view the time of the operation based on Count

count:681719 (5)

————————————————-Com.anenjoy.manage.mapper.TblUserTempMapper.insert

Insert into Tblusertemp (Activecode, PASSWORDMD5, Passwordrandomkey,

PHONE, EMAIL, USERNAME,

Awartar, STATUS, CreateDate,

USERID, IMSI, Checkcode

)

VALUES (?,?,?,

?, ?, ?,

?, ?, ?,

?, ?, ?

)

count:129557388

————————————————-Com.anenjoy.manage.mapper.TblUserTempMapper.selectByExample

Select

Activecode, PASSWORDMD5, Passwordrandomkey, PHONE, EMAIL, USERNAME, Awartar, STATUS,

CreateDate, USERID, IMSI, Checkcode

From Tblusertemp

count:333938203

count:560704

And then these are basically OK

Original address: Http://www.darrenzhong.com/?p=73#codesyntax_6

SOURCE Download: Http://pan.baidu.com/s/1jGnATds decompression password: Darren Blog

Summary: I have tried both of these ways, and have tested the efficiency of both ways. Here is a personal view, and if there are other points of view welcome discussion.

Queries using Ehcache+mybatis are less efficient than springmvc+mybatis+ehcache.

The <diskstore path= "Java.io.tmpdir"/> Node in the Ehcache.xml configuration file is more efficient at using the system default cache location than using the custom cache location Path= "D:\cache".

Finally attached to my demo source code: Source Download

The source code Riga database SQL file directly import can, before also tried the MyBatis database of read and write separation. But they are all independent and do not affect each other. Next time write an article about the separation of database read and write.




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.