---
title: "hibernate-memcached--在Hibernate中使用Memcached作为一个二级分"
author: "liuye"
date: "2008-12-19T18:00:30Z"
tags: []
slug: "hibernate-memcached-hibernate-memcached"
draft: false
lastmod: "2008-12-19T18:00:30Z"
id: 297712
url: "https://liuye.iteye.com/blog/297712"
---

今天在网上看到一个用Memcached作为Hibernate二级分布式缓存，感觉挺有兴趣，就是尝试用了，感觉还不错，就推荐给大家看一下。

官方网址: [http://code.google.com/p/hibernate-memcached/](http://code.google.com/p/hibernate-memcached/)  
目前最新版本为1.0, 支持Hibernate3.3.

下面是具体的使用方法:  
hibernate-memcached需要支持的类库如下:

- hibernate-memcached-&lt;version&gt;.jar
- [memcached-2.1.jar](http://spymemcached.googlecode.com/files/memcached-2.1.jar)
- [spy-2.4.jar](http://bleu.west.spy.net/%7Edustin/repo/spy/jars/spy-2.4.jar)
- [commons-codec 1.3](http://repo1.maven.org/maven2/commons-codec/commons-codec/1.3/commons-codec-1.3.jar)
- [slf4j-1.5.0.jar 如果你的项目使用的是Log4j,可以用](http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.5.0/slf4j-api-1.5.0.jar) [slf4j-log4j12-1.5.0.jar](http://repo1.maven.org/maven2/org/slf4j/slf4j-log4j12/1.5.0/slf4j-log4j12-1.5.0.jar)

配置方法如下：

配置Hibernate使用cache提供类

hibernate.cache.provider\_class com.googlecode.hibernate.memcached.MemcachedCacheProvider

 

[设置查询缓存开启]()

hibernate.cache.use\_query\_cache true

其它一些参数设置说明:

**Property** **Default** **Description** hibernate.memcached.servers localhost:11211 memcached 服务地址，多个用空格分隔  
格式host:port  
hibernate.memcached.cacheTimeSeconds 300 缓存失效时间,单位秒 hibernate.memcached.keyStrategy HashCodeKeyStrategy 缓存Key生成存储HashCode算法 hibernate.memcached.readBufferSize DefaultConnectionFactory.DEFAULT\_READ\_BUFFER\_SIZE 从服务器读取数据缓存区大小  
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 操作超时时间设置 hibernate.memcached.hashAlgorithm HashAlgorithm.KETAMA\_HASH 新增缓存数据到服务器时使用的Hash散列算法。 当 hibernate-memcached 设置成 KETAMA\_HASH算法时,注意:默认客户端API使用的是 HashAlgorithm.NATIVE\_HASH hibernate.memcached.clearSupported false 支持MemcachedCache.clear()方法清空缓存。  
建议不要开启。

配置示例(本文以Hibernate3.3-entitymanager为例)  
配置 persistence.xml文件

&lt;? xml version="1.0" encoding="UTF-8" ?&gt;  
&lt; persistence  xmlns ="http://java.sun.com/xml/ns/persistence" " target ="\_new" &gt; 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"&gt;

&lt; persistence-unit  name ="entityManager"  transaction-type ="RESOURCE\_LOCAL" &gt;  
&lt; provider &gt; org.hibernate.ejb.HibernatePersistence &lt;/ provider &gt;  
&lt; jta-data-source &gt; java:comp/env/jdbc/qualitydb &lt;/ jta-data-source &gt;

&lt; properties &gt;  
&lt; property  name ="hibernate.dialect"  value ="org.hibernate.dialect.PostgreSQLDialect"   /&gt;  
&lt; property  name ="hibernate.max\_fetch\_depth"  value ="3"   /&gt;  
&lt; property  name ="hibernate.show\_sql"  value ="true"   /&gt;

&lt; property  name ="hibernate.cache.region\_prefix"  value ="quality.cache.ehcache" /&gt;  
&lt; property  name ="hibernate.cache.use\_second\_level\_cache"  value ="true" /&gt;  
&lt; property  name ="hibernate.cache.use\_structured\_entries"  value ="true" /&gt;  
&lt; property  name ="hibernate.cache.use\_query\_cache"  value ="true" /&gt;  
&lt; property  name ="hibernate.cache.provider\_class"  value ="com.googlecode.hibernate.memcached.MemcachedCacheProvider" /&gt;  
&lt; property  name ="hibernate.memcached.servers"  value ="localhost:11211" /&gt;

&lt;/ properties &gt;

&lt;/ persistence-unit &gt;

&lt;/ persistence &gt;

启动后，提示如下：  
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 sun.nio.ch.SelectionKeyImpl@16e59da

表示我们第一步配置已经成功了，接下来，对需要进行缓存的Entity进行配置

 1  @Entity  
 2  @Cache(usage  =  CacheConcurrencyStrategy.READ\_WRITE) // 设置要求缓存  
 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      @OneToMany  
13      @Cache(usage  =  CacheConcurrencyStrategy.READ\_WRITE)  
14       private  Set &lt; Book &gt;  books;  
15    
16  }

Ok,现在配置已经完成。