spring+mybatis+ehcache配置キャッシュ


1:スプリングロードehcacheプロファイル
  <!--   ehcache   -->    
   <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
     <property name="configLocation" value="classpath:ehcache.xml" />  
   </bean>
2:ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>  
<ehcache>
  <!-- maxElementsInMemory:              maxInMemory:             。 eternal:    (  :     )      。   ,              。 timeToIdleSeconds:              。 timeToLiveSeconds:             . overflowToDisk:           maxInMemory              。 memoryStoreEvictionPolicy:   maxElementsInMemory   ,Ehcache              。     LRU(      )。      FIFO(    )  LFU(    )。 diskPersistent:            。 -->

    <diskStore path="java.io.tmpdir"/>
    <defaultCache maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" diskPersistent="false" />

    <cache name="districtDataCache" maxElementsInMemory="4000" eternal="true" overflowToDisk="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU"/>    
</ehcache>
上のdisk Storr pathはある経路を指定できます.java.io.tmpdirはあなたのシステムのキャッシュディレクトリを指します.
3:mapper.xml
その後、対応するmapper.xmlに加えます.
<cache readOnly="true">
  <property name="timeToIdleSeconds" value="3600"/><!--1 hour-->  
    <property name="timeToLiveSeconds" value="3600"/><!--1 hour-->  
    <property name="maxEntriesLocalHeap" value="1000"/>  
    <property name="maxEntriesLocalDisk" value="10000000"/>  
    <property name="memoryStoreEvictionPolicy" value="LRU"/>  
  </cache>
(1)propertyパラメータの配置は無添加でもいいです.デフォルト値があります.みんなも全部でどのような配置があるか調べて、自分の必要に応じて配置してください.そしてこの配置はcacheを持って実行するログです.ログを持たないなら、ロギングEhcacheをEhcache Cacheに変えてもいいです.(2)readOnlyがfalseである場合、結果セットのオブジェクトは、順序付け可能である必要があります.本体オブジェクトをimplements Serializableにする必要があります.
4:useCacheスイッチ
mapper.xmlのようにデフォルトではすべての操作でキャッシュポリシーを実行します.もしいくつかのsqlが実行する必要がないなら、useCacheをfalseに設定することができます.

<select id="selectUser" resultMap="BaseResultMap" parameterType="XX.XX.XX.XX.User" useCache="false" >
リボン:
実はmybatisキャッシュは一番いい選択ではありません.a、一定規模のデータ量に対して、内蔵のcache方式は役に立ちません.b、検索結果集をキャッシュすることはMyBatisフレームが得意ではないです.一心にやっているのはsql mapperです.このフレームを採用したアプリはキャッシュを構築するのがより合理的で、例えばOCache、Memcachedなどを採用します.