Hibernate キャッシュ - 2次キャッシュとクエリキャッシュの設定及び使い方
2008/08/12 22:19Update
Hibernateの1次キャッシュと違って、2次キャッシュデフォルトでは有効になっていません。本文は、2次キャッシュ及びクエリキャッシュの設定方法や使い方について学びます。
目次
◇ Hibernate 2次キャッシュの設定方法
◇ 実験してみる
◇ 実行ログから確認
◇ サンプルソース
Hibernate キャッシュ機能概要
からHibernateキャッシュの基本を理解してください。Hibernateは柔軟性に優れたキャッシュの仕組みを用意しています。
簡単な設定を行うことで、Hibernate自身が提供するキャッシュの簡易実装を利用することも、EHCacheやOSCacheなどの外部キャッシュ機構や自作のキャッシュ機構も手軽に導入することができます。
◇ Hashtable:Hibernateが提供するキャッシュの簡易実装。
◇ EHCache
◇ OSCache
◇ SwarmCache
◇ JbossTreeCache

本文はEHCacheを使ってHibernate2次キャッシュ/クエリキャッシュの設定方法及びその使い方について説明します。
今回は次のテーブルを例とします。
テーブル定義
create table FATHER ( ID integer not null, NAME varchar(100), primary key (ID) ); create table SON ( id integer not null, NAME varchar(100), FATHER_ID integer not null, primary key (id) ); alter table SON add index FK1417282DD016B (FATHER_ID), add constraint FK1417282DD016B foreign key (FATHER_ID) references FATHER (ID);
※FATHERはSONと1対多の関係
2次キャッシュの設定
2次キャッシュを有効にするには、hibernate.cache.provider_classでキャッシュのプロバイダー・クラスを指定する必要があります。
hibernate.cfg.xmlのキャッシュ設定例
hibernate.cfg.xml<hibernate-configuration> <session-factory> <!-- 抜粋 --> <!-- SQL出力 --> <property name="show_sql">true</property> <!-- クエリキャッシュの設定 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- キャッシュのプロバイダーの設定 --> <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </property> <!-- Mapping files --> <mapping resource="Father.hbm.xml"/> <mapping resource="Son.hbm.xml"/> <!-- クラスレベルのキャッシュ --> <class-cache class="eg.Father" usage="nonstrict-read-write"/> <class-cache class="eg.Son" usage="nonstrict-read-write"/> <!-- コレクションレベルのキャッシュ --> <collection-cache collection="eg.Father.sonSet" usage="read-write"/> </session-factory> </hibernate-configuration>
※hibernate.cache.use_query_cacheはtrueを設定することで、クエリキャッシュを有効にします。クエリキャッシュについて、後ほど説明します。
※hibernate.cfg.xmlの中に、<class-cache> 及び <collection-cache>要素で クラス・コレクション レベルのキャッシュを定義します。
※<class-cache>要素のclassプロパティには、エンティティの パッケージ名・クラス名 を指定します。
※<collection-cache>要素のcollectionプロパティには、クラス名・コレクション の名前を指定します。
また、2次キャッシュを適用したい永続クラスのマッピング定義ファイル(*.hbm.xml)中にある<class>要素、<set>や<array>などのコレクション要素内に、<cache usage="キャッシュ戦略" ...>の形でも指定できます。
この場合、別途hibernate.cfg.xmlの中に<class-cache>、<collection-cache>で指定する必要がありません。
マッピング定義ファイルのキャッシュ定義例:
Father.hbm.xml<hibernate-mapping> <class name="eg.Father" table="FATHER"> <!-- クラスレベルのキャッシュ --> <cache usage="read-write"/> <id name="id" unsaved-value="0" column="ID"> <generator class="increment"/> </id> <set name="sonSet" ...> <!-- コレクションレベルのキャッシュ --> <cache usage="read-write"/> <key column="FATHER_ID"/> <one-to-many class="eg.Son"/> </set> ... </class> </hibernate-mapping>
<cache>要素は次の形式になります。
<cache usage="transactional|read-write|nonstrict-read-write|read-only" (1) region="RegionName" (2) include="all|non-lazy" (3) />
◇ read-only戦略
読込みのみ用のキャッシュ戦略で、最も効率のよい戦略です。
<class name="eg.NameConst" ...>
<cache usage="read-only"/>
....
</class>
◇ read-write戦略
<class name="eg.Cat" .... >
<cache usage="read-write"/>
....
<set name="kittens" ... >
<cache usage="read-write"/>
....
</set>
</class>
◇ nonstrict-read-write
◇ transactional
========= Ehcache 独自の設定 ==============
Ehcacheの場合、classpathに通したehcache.xmlファイルを追加することで、より細かく設定することができます。
ehcache.xmlの設定例:
ehcache.xml
<ehcache> <diskStore path="/usr/local/cache/hibernate/"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="true" /> <!-- cache hibernate entity --> <cache name="eg.Father" maxElementsInMemory="50" eternal="true" overflowToDisk="false" /> <cache name="eg.Son" maxElementsInMemory="300" eternal="true" overflowToDisk="false" /> <cache name="eg.Father.sonSet" maxElementsInMemory="100" eternal="true" overflowToDisk="false" /> </ehcache>
ここでは、ehcache.xmlの解説を省きます。
次のページ:実験してみる
Sponsored Link
- Relative Articles
- Hibernate設定ファイルその1 - hibernate.cfg.xml - (2008/07/09 14:08)
- Hibernate c3p0 - よく利用されているコネクションプーリング - (2008/07/09 15:00)
- Hibernate 方言(Dialect)一覧 - (2008/07/09 15:10)
- Hibernate のSessionとSessionFactory - (2008/07/09 15:51)
- ThreadLocal でHibernate Session を効率的に管理する - (2008/07/09 18:43)
- Hibernateで複数のDBに接続するには? - (2008/07/09 18:47)
- Hibernateとは - (2008/07/11 14:23)
- Hibernate のインストール - (2008/07/24 11:01)
- Hibernate の開発手順 - (2008/07/24 12:44)
- Hibernate SQLをログに出力方法 - (2008/07/30 16:06)