Hibernate-memcached-use memcached as a level-2 distributed cache in hibernate

Source: Internet
Author: User

Today, I saw a memcached on the Internet as the hibernate level-2 distributed cache. I felt very interested. I tried it and it felt pretty good. I recommend it to you.

Http://code.google.com/p/hibernate-memcached/

The latest version is 1.0, supporting hibernate3.3.

The following describes how to use it:
Hibernate-memcached must support the following class libraries:

  • Hibernate-memcached-<version>. Jar
  • Memcached-2.1.jar
  • Spy-2.4.jar
  • Commons-codec 1.3
  • Slf4j-1.5.0.jar if your project uses log4j, you can use
    Slf4j-log4j12-1.5.0.jar


The configuration method is as follows:

Configure hibernate to use cache to provide classes

Hibernate. cache. provider_class Com. googlecode. hibernate. memcached. memcachedcacheprovider

 

Enable query Cache

Hibernate. cache. use_query_cache True


Other parameter settings:

Property Default Description
Hibernate. memcached. Servers Localhost: 11211 Memcached service address, separated by Spaces
Format: Host: Port
Hibernate. memcached. cachetimeseconds 300 Cache expiration time, in seconds
Hibernate. memcached. keystrategy Hashcodekeystrategy Cache key generation and storage hashcode Algorithm
Hibernate. memcached. readbuffersize Defaultconnectionfactory. default_read_buffer_size Size of the data cache zone read from the server
Hibernate. memcached. operationqueuelength Defaultconnectionfactory. default_op_queue_len Maximum length of the Operation queue returned by this connection Factory
Hibernate. memcached. operationtimeout Defaultconnectionfactory. default_operation_timeout Operation timeout settings
Hibernate. memcached. hashalgorithm Hashalgorithm. ketama_hash The Hash hash algorithm used to cache data to the server is added. When
When setting hibernate-memcached to the ketama_hash algorithm, note: the Default Client API uses hashalgorithm. native_hash.
Hibernate. memcached. clearsupported False Supports the memcachedcache. Clear () method to clear the cache.
Do not enable this function.


Configuration example (this document uses hibernate3.3-entitymanager as an example)
Configure the persistence. xml file

<?
XML version = "1.0" encoding = "UTF-8"
?>


<
Persistence
Xmlns
= "Http://java.sun.com/xml/ns/persistence"
"Target
= "_ New"
>
Http://java.sun.com/xml/ns/persistence "xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "xsi: schemalocation =" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd "version =" 1.0 ">


<
Persistence-Unit
Name
= "Entitymanager"
Transaction-type
= "Resource_local"
>


<
Provider
>
Org. hibernate. EJB. hibernatepersistence
</
Provider
>


<
JTA-data-Source
>
Java: COMP/ENV/jdbc/qualitydb
</
JTA-data-Source
>


<
Properties
>


<
Property
Name
= "Hibernate. dialect"
Value
= "Org. hibernate. dialect. postgresqldialect"
 
/>


<
Property
Name
= "Hibernate. max_fetch_depth"
Value
= "3"
 
/>


<
Property
Name
= "Hibernate. show_ SQL"
Value
= "True"
 
/>


<
Property
Name
= "Hibernate. cache. region_prefix"
Value
= "Quality. cache. ehcache"
/>


<
Property
Name
= "Hibernate. cache. use_second_level_cache"
Value
= "True"
/>


<
Property
Name
= "Hibernate. cache. use_structured_entries"
Value
= "True"
/>


<
Property
Name
= "Hibernate. cache. use_query_cache"
Value
= "True"
/>


<
Property
Name
= "Hibernate. cache. provider_class"
Value
= "Com. googlecode. hibernate. memcached. memcachedcacheprovider"
/>


<
Property
Name
= "Hibernate. memcached. servers"
Value
= "Localhost: 11211"
/>


</
Properties
>


</
Persistence-Unit
>


</
Persistence
>

The following prompt is displayed after startup:
17:10:08, 312 jclloggeradapter. java265 info -- Starting memcachedclient...
2008-08-28
17:10:08. 718 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
2008-08-28
17:10:08. 750 Info net. Spy. memcached. memcachedconnection: Connection
State changed for sun. NiO. Ch. selectionkeyimpl @ 16e59da

Indicates that the first step has been configured successfully. Next, configure the entity to be cached.

1
@ Entity

2
@ Cache (usage
=
Cacheconcurrencystrategy. read_write)
//
Set Request Cache


3
Public
 
Class
Student {

4
 

5
@ ID

6
@ Column (Length
=
32
)

7

Private
String ID;

8


9
@ Column (Length
=
20
)

10

Private
String name;

11


12
@ Onetoworkflow

13
@ Cache (usage
=
Cacheconcurrencystrategy. read_write)

14

Private
Set
<
Book
>
Books;

15
 

16
}

OK. Now the configuration is complete.

Good luck!
Yours Matthew!

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.