在Hibernate中使用Memcached作為一個二級分布式緩衝

來源:互聯網
上載者:User

標籤:des   blog   http   java   get   使用   

轉自:http://www.blogjava.net/xmatthew/archive/2008/08/20/223293.html hibernate-memcached--在Hibernate中使用Memcached作為一個二級分布式緩衝

今天在網上看到一個用Memcached作為Hibernate二級分布式緩衝,感覺挺有興趣,就是嘗試用了,感覺還不錯,就推薦給大家看一下。

官方網址: http://code.google.com/p/hibernate-memcached/
目前最新版本為1.0, 支援Hibernate3.3.

下面是具體的使用方法:
hibernate-memcached需要支援的類庫如下:

  • hibernate-memcached-<version>.jar
  • memcached-2.1.jar
  • spy-2.4.jar
  • commons-codec 1.3
  • slf4j-1.5.0.jar 如果你的項目使用的是Log4j,可以用 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檔案

<?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>



啟動後,提示如下:
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]

表示我們第一步配置已經成功了,接下來,對需要進行緩衝的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<Book> books;
15 
16 }



Ok,現在配置已經完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.