Transferred from: http://www.blogjava.net/xmatthew/archive/2008/08/20/223293.html hibernate-memcached--using memcached as a level two distributed cache in hibernate
Today on the Internet to see a use memcached as hibernate two level distributed cache, feel very interested, is trying to use, feel good, recommend to everyone to see.
Official website: http://code.google.com/p/hibernate-memcached/
Currently the latest version is 1.0, supporting Hibernate3.3.
Here are the specific ways to use it:
The class libraries that hibernate-memcached need to support are as follows:
- Hibernate-memcached-<version>.jar
- Memcached-2.1.jar
- Spy-2.4.jar
- Commons-codec 1.3
- Slf4j-1.5.0.jar If your project is using log4j, you can use the Slf4j-log4j12-1.5.0.jar
The configuration method is as follows:
Configure hibernate to use the cache to provide classes
Hibernate.cache.provider_class |
Com.googlecode.hibernate.memcached.MemcachedCacheProvider |
Set query cache to open
Hibernate.cache.use_query_cache |
True |
Some other parameter setting instructions:
Property |
Default |
Description |
Hibernate.memcached.servers |
localhost:11211 |
memcached service address, multiple spaces separated Format Host:port
|
Hibernate.memcached.cacheTimeSeconds |
300 |
Cache expiration time, in seconds |
Hibernate.memcached.keyStrategy |
Hashcodekeystrategy |
Cache key Generation Storage Hashcode algorithm |
Hibernate.memcached.readBufferSize |
Defaultconnectionfactory.default_read_buffer_size |
Read data buffer size from 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 Time-out setting |
Hibernate.memcached.hashAlgorithm |
Hashalgorithm.ketama_hash |
The hash hash algorithm used when caching data to the server is added. When Hibernate-memcached is set to the Ketama_hash algorithm, note: The default client API uses Hashalgorithm.native_hash |
hibernate.memcached.clearSupported |
False |
The Memcachedcache.clear () method is supported to empty the cache. It is not recommended to open. |
Configuration examples (this article takes Hibernate3.3-entitymanager as an example)
Configuring 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>
When you start, you are prompted as follows:
2008-08-28 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 [email protected]
Indicates that our first step configuration has succeeded, and next, configure the entity that needs to be cached
1 @Entity
2 @Cache (usage = cacheconcurrencystrategy.read_write)//setting requires caching
3 public class Student {
4
5 @Id
6 @Column (LENGTH=32)
7 Private String ID;
8
9 @Column (LENGTH=20)
Ten private string name;
11
@OneToMany
@Cache (usage = cacheconcurrencystrategy.read_write)
+ Private set<book> books;
15
16}
Ok, the configuration is now complete.