mybatis學習筆記(16)-mybatis整合ehcache

mybatis學習筆記(16)-mybatis整合ehcache

標籤: mybatisjava


[TOC]git


ehcache是一個分佈式緩存框架github

分佈緩存

咱們系統爲了提升系統併發,性能、通常對系統進行分佈式部署(集羣部署方式)apache

分佈緩存

不使用分佈緩存,緩存的數據在各各服務單獨存儲,不方便系統開發。因此要使用分佈式緩存對緩存數據進行集中管理。緩存

mybatis沒法實現分佈式緩存,須要和其它分佈式緩存框架進行整合。mybatis

整合方法(掌握)

mybatis提供了一個cache接口,若是要實現本身的緩存邏輯,實現cache接口開發便可。併發

mybatis和ehcache整合,mybatis和ehcache整合包中提供了一個cache接口的實現類。app

package org.apache.ibatis.cache;

import java.util.concurrent.locks.ReadWriteLock;

/**
 * SPI for cache providers.
 * 
 * One instance of cache will be created for each namespace.
 * 
 * The cache implementation must have a constructor that receives the cache id as an String parameter.
 * 
 * MyBatis will pass the namespace as id to the constructor.
 * 
 * <pre>
 * public MyCache(final String id) {
 *  if (id == null) {
 *    throw new IllegalArgumentException("Cache instances require an ID");
 *  }
 *  this.id = id;
 *  initialize();
 * }
 * </pre>
 *
 * @author Clinton Begin
 */

public interface Cache {

  /**
   * @return The identifier of this cache
   */
  String getId();

  /**
   * @param key Can be any object but usually it is a {@link CacheKey}
   * @param value The result of a select.
   */
  void putObject(Object key, Object value);

  /**
   * @param key The key
   * @return The object stored in the cache.
   */
  Object getObject(Object key);

  /**
   * Optional. It is not called by the core.
   * 
   * @param key The key
   * @return The object that was removed
   */
  Object removeObject(Object key);

  /**
   * Clears this cache instance
   */  
  void clear();

  /**
   * Optional. This method is not called by the core.
   * 
   * @return The number of elements stored in the cache (not its capacity).
   */
  int getSize();
  
  /** 
   * Optional. As of 3.2.6 this method is no longer called by the core.
   *  
   * Any locking needed by the cache must be provided internally by the cache provider.
   * 
   * @return A ReadWriteLock 
   */
  ReadWriteLock getReadWriteLock();

}

mybatis默認實現cache類是:框架

package org.apache.ibatis.cache.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.cache.CacheException;

/**
 * @author Clinton Begin
 */
public class PerpetualCache implements Cache {

  private String id;

  private Map<Object, Object> cache = new HashMap<Object, Object>();

  public PerpetualCache(String id) {
    this.id = id;
  }

  public String getId() {
    return id;
  }

  public int getSize() {
    return cache.size();
  }

  public void putObject(Object key, Object value) {
    cache.put(key, value);
  }

  public Object getObject(Object key) {
    return cache.get(key);
  }

  public Object removeObject(Object key) {
    return cache.remove(key);
  }

  public void clear() {
    cache.clear();
  }

  public ReadWriteLock getReadWriteLock() {
    return null;
  }

  public boolean equals(Object o) {
    if (getId() == null) throw new CacheException("Cache instances require an ID.");
    if (this == o) return true;
    if (!(o instanceof Cache)) return false;

    Cache otherCache = (Cache) o;
    return getId().equals(otherCache.getId());
  }

  public int hashCode() {
    if (getId() == null) throw new CacheException("Cache instances require an ID.");
    return getId().hashCode();
  }

}

整合ehcache

  • 加入ehcache包
    • ehcache-core-2.6.5.jar
    • mybatis-ehcache-1.0.2.jar

配置mapper中cache中的type爲ehcache對cache接口的實現類型分佈式

<!-- 開啓本mapper的namespace下的二級緩存
    type:指定cache接口的實現類的類型,mybatis默認使用PerpetualCache
    要和ehcache整合,須要配置type爲ehcache實現cache接口的類型
    <cache />
    -->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

加入ehcache的配置文件

在classpath下配置ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
	<diskStore path="F:\develop\ehcache" />
	<defaultCache 
		maxElementsInMemory="1000" 
		maxElementsOnDisk="10000000"
		eternal="false" 
		overflowToDisk="false" 
		timeToIdleSeconds="120"
		timeToLiveSeconds="120" 
		diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
	</defaultCache>
</ehcache>

做者@brianway更多文章:我的網站 | CSDN | oschina

相關文章
相關標籤/搜索