标签: 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(); } }
配置mapper中cache
中的type
为ehcache对cache接口的实现类型分布式
<!-- 开启本mapper的namespace下的二级缓存 type:指定cache接口的实现类的类型,mybatis默认使用PerpetualCache 要和ehcache整合,须要配置type为ehcache实现cache接口的类型 <cache /> --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
在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>