今天在網(wǎng)上看到一個(gè)用Memcached作為Hibernate二級(jí)分布式緩存,感覺挺有興趣,就是嘗試用了,感覺還不錯(cuò),就推薦給大家看一下。
官方網(wǎng)址:
http://code.google.com/p/hibernate-memcached/
目前最新版本為1.0,
支持Hibernate3.3.
下面是具體的使用方法:
hibernate-memcached需要支持的類庫如下:
配置方法如下:
配置Hibernate使用cache提供類
hibernate.cache.provider_class |
com.googlecode.hibernate.memcached.MemcachedCacheProvider |
設(shè)置查詢緩存開啟
hibernate.cache.use_query_cache |
true |
其它一些參數(shù)設(shè)置說明:
Property
|
Default
|
Description
|
hibernate.memcached.servers
|
localhost:11211
|
memcached
服務(wù)地址,多個(gè)用空格分隔
格式host:port
|
hibernate.memcached.cacheTimeSeconds
|
300
|
緩存失效時(shí)間,單位秒 |
hibernate.memcached.keyStrategy
|
HashCodeKeyStrategy
|
緩存Key生成存儲(chǔ)HashCode算法 |
hibernate.memcached.readBufferSize
|
DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE
|
從服務(wù)器讀取數(shù)據(jù)緩存區(qū)大小
|
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
|
操作超時(shí)時(shí)間設(shè)置 |
hibernate.memcached.hashAlgorithm
|
HashAlgorithm.KETAMA_HASH
|
新增緩存數(shù)據(jù)到服務(wù)器時(shí)使用的Hash散列算法。
當(dāng) hibernate-memcached 設(shè)置成 KETAMA_HASH算法時(shí),注意:默認(rèn)客戶端API使用的是
HashAlgorithm.NATIVE_HASH |
hibernate.memcached.clearSupported
|
false
|
支持MemcachedCache.clear()方法清空緩存。
建議不要開啟。
|
配置示例(本文以Hibernate3.3-entitymanager為例)
配置
persistence.xml文件
<?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>
啟動(dòng)后,提示如下:
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
表示我們第一步配置已經(jīng)成功了,接下來,對(duì)需要進(jìn)行緩存的Entity進(jìn)行配置
1 @Entity
2 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)//設(shè)置要求緩存
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<Book> books;
15
16 }
Ok,現(xiàn)在配置已經(jīng)完成。
Good
Luck!
Yours Matthew!
posted on 2008-12-13 02:10
sw0rd 閱讀(178)
評(píng)論(0) 編輯 收藏