標籤:
1. 下載memcached的windows穩定版,
(本次測試使用下載memcached http://www.newasp.net/soft/63735.html)
2. 在CMD下進入memcached解壓路徑:輸入 memcached.exe -d install" 安裝.
3. 再輸入:memcached.exe -d start" 啟動,可以看到進程中多了memcached。
NOTE:1、 一定要開啟memcached服務;2、以後memcached將作為windows的一個服務每次開機時自動啟動。
二、導包
commons-codec-1.3.jar
hibernate-memcached-1.2.2.jar(很多文章中少了這個,導致失敗!)
memcached-2.1.jar
spy-2.4.jar
slf4j-api-1.5.0.jar
三、修改設定檔
1、applicationContext.xml
<property name="hibernateProperties"> <!-- hibernate memcached 二級緩衝 --> <value> hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.query.substitutions=true 1, false 0 hibernate.jdbc.batch_size=20<!--預設的資料庫--> <!--配置Hibernate使用cache提供類--> hibernate.cache.provider_class=com.googlecode.hibernate.memcached.MemcachedCacheProvider <!-- 開啟Hibernate的二級緩衝 --> hibernate.cache.use_second_level_cache=true <!--設定查詢快取開啟--> hibernate.cache.use_query_cache=true <!-- 設定memcached快取服務器的連接埠 --> hibernate.memcached.servers=localhost:11211 <!-- 設定二級緩衝的首碼名稱 --> hibernate.cache.region_prefix=quality.cache.ehcache <!-- 否使用結構化的方式緩衝對象 --> hibernate.cache.use_structured_entries=true <!-- 操作逾時時間設定,單位ms --> hibernate.memcached.operationTimeout=300000 <!-- 緩衝失效時間,單位秒 --> hibernate.memcached.cacheTimeSeconds=300 </value> </property>
更多詳細的參數請參考http://code.google.com/p/hibernate-memcached/wiki/Configuration
此時啟動tomcat伺服器,console假如有如下的資訊,
2013-07-20 18:29:42.426 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
2013-07-20 18:29:42.426 INFO net.spy.memcached.MemcachedConnection: Connection state changed for [email protected]
那恭喜您,您已經成功配置memcached作為二級緩衝了!但是此時還不能使用memcached。
2、修改資料表對應檔*.hbm.xml
只需在需要緩衝的欄位前加上<cache usage="read-write"/>,根據不同的需求,usage可改為read、write
<hibernate-mapping package="com.xxx.xxx.xxx"> <class name="xxx" table="xxx_xxx"> <cache usage="read-only" /> <id name="id"> <generator class="native" /> </id> <property generated="never" lazy="false" name="title" /> <property generated="never" lazy="false" length="50000" name="content" type="text" /> <property generated="never" lazy="false" name="faceIcon" /> <property generated="never" lazy="false" name="postTime" type="timestamp" /> <property generated="never" lazy="false" name="ipAddr" />
3、查詢語句前
加上query.setCacheable(true);//這樣才能使查詢的時候調用緩衝!要對查詢設計到的所有方法添加開啟二級緩衝
OK,此時應該是可以使用memcached作為強大的二級緩衝了!
四、案例
第一次點擊搜尋按鈕,console輸出資訊為
Hibernate: select department0_.id as id21_0_, department0_.name as name21_0_, department0_.principal as principal21_0_, department0_.description as descript4_21_0_, department0_.parentId as parentId21_0_ from suma_department department0_ where department0_.id=?Hibernate: select department0_.id as id21_0_, department0_.name as name21_0_, department0_.principal as principal21_0_, department0_.description as descript4_21_0_, department0_.parentId as parentId21_0_ from suma_department department0_ where department0_.id=?
第二、三、四...次點擊搜尋按鈕,console輸出資訊為:
就沒有sql語句的列印資訊:頁面依舊正常能查詢到資料,說明memcached二級緩衝添加成功
Ok,第一次之後的查詢在緩衝生命週期內都不用到資料庫去取資料了。
騷年:已經成功了,還等什嗎?
參考地址忘記了 :-----
SSH整合緩衝之-Memcached作為hibernate的二級緩衝