SSH integrated Cache-memcached as Hibernate's Level two cache

Source: Internet
Author: User
Tags tomcat server

1. Download Memcached's Windows stable version,

(This test uses download memcached http://www.newasp.net/soft/63735.html)
2. Under CMD, enter the memcached decompression path: Enter memcached.exe-d install.


3. Re-enter: memcached.exe-d start ", you can see more memcached in the process.

Note:1, must open the memcached service; 2. memcached will start automatically every time a service is turned on for Windows.

Second, guide package

Commons-codec-1.3.jar

Hibernate-memcached-1.2.2.jar (This is missing in many articles, which leads to failure!) )

Memcached-2.1.jar

Spy-2.4.jar

Slf4j-api-1.5.0.jar

Third, modify the configuration file

1, Applicationcontext.xml

 <property name= "Hibernateproperties" > <!--hibernate memcached level Two cache--<value> Hibernate.dialect=org.hibernate.dialect.mysqlinnodbdialect hibernate.show_sql=true Hibernat E.format_sql=true Hibernate.query.substitutions=true 1, false 0 hibernate.jdbc.batch_size=20<!-- Database--<!--configuration Hibernate uses the cache to provide class--hibernate.cache.provider_class=com.googlecode.hibe Rnate.memcached.MemcachedCacheProvider <!--Open Hibernate's Level two cache--Hibernate.cache.use_second_ Level_cache=true <!--set query cache on-hibernate.cache.use_query_cache=true <!--setup Mem                Cached cache server Port--hibernate.memcached.servers=localhost:11211 <!--set the prefix name for level two cache--               Hibernate.cache.region_prefix=quality.cache.ehcache <!--No use structured caching of objects-- Hibernate.cache.use_stRuctured_entries=true <!--operation Timeout setting, units MS-to hibernate.memcached.operationtimeout=300000 <!--cache expiration time, in seconds--hibernate.memcached.cachetimeseconds=300 </value> &     Lt;/property>

For more detailed parameters please refer to Http://code.google.com/p/hibernate-memcached/wiki/Configuration

At this point, the Tomcat server is started, if the console has the following information,

2013-07-20 18:29:42.426 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=localhost/127.0.0.1:11211, #Rops = 0, # Wops=0, #iq =0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue
2013-07-20 18:29:42.426 INFO net.spy.memcached.MemcachedConnection:Connection state changed for [email protected]< /c0>

Congratulations, you have successfully configured memcached as level two cache! However, you cannot use memcached at this time.

2. Modify the Data table mapping file *.hbm.xml

Simply add the <cache usage= "Read-write"/> to the field you want to cache, and depending on your needs, usage can be changed to read, write

<hibernate-mapping Package= "Com.xxx.xxx.xxx">    <classname= "XXX"Table= "Xxx_xxx">        <Cacheusage= "Read-only" />        <IDname= "id">            <Generatorclass= "Native" />        </ID>        < Propertygenerated= "Never"Lazy= "false"name= "title" />        < Propertygenerated= "Never"Lazy= "false"length= "50000"name= "Content"type= "text" />        < Propertygenerated= "Never"Lazy= "false"name= "Faceicon" />        < Propertygenerated= "Never"Lazy= "false"name= "Posttime"type= "Timestamp" />        < Propertygenerated= "Never"Lazy= "false"name= "IPAddr" />
3, before the query statement

Add query.setcacheable (TRUE);//To make the query call the cache! To add an open level two cache to all methods that the query designs to

OK, you should be able to use memcached as a powerful two-level cache now!

Iv. Cases

The first time you click the Search button, the console output information is

Hibernate:Selectdepartment0_.id asid21_0_, Department0_.name asname21_0_, Department0_.principal asprincipal21_0_, Department0_.description asdescript4_21_0_, Department0_.parentid asparentid21_0_ fromsuma_department department0_wheredepartment0_.id=? Hibernate:Selectdepartment0_.id asid21_0_, Department0_.name asname21_0_, Department0_.principal asprincipal21_0_, Department0_.description asdescript4_21_0_, Department0_.parentid asparentid21_0_ fromsuma_department department0_wheredepartment0_.id=?

Combined second ... Click the Search button, the console output information is:

There is no SQL statement printing information: The page is still normal to query data, indicating that the memcached two cache added successfully

Ok, the query after the first time does not have to go to the database to fetch the data in the cache life cycle.

SAO years: Already successful, what are you waiting for?

The reference address forgot:-----

SSH integrated Cache-memcached as Hibernate's Level two 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.