MyBatis Study notes (-mybatis) integration Ehcache

Source: Internet
Author: User

MyBatis Study notes (-mybatis) integration Ehcache

    • MyBatis Study notes 14-mybatis integration Ehcache
      • Distributed cache
      • Integration Method Mastery
        • Integrated Ehcache
        • Add the Ehcache configuration file

Ehcache is a distributed cache framework

Distributed cache

Our system to improve the system concurrency, performance, general Distributed system Deployment (cluster deployment mode)

Without the use of distributed caches, cached data is stored separately in each service and is inconvenient for system development. Therefore, the cached data is centrally managed using distributed caching.

MyBatis cannot implement distributed caching, and it needs to be integrated with other distributed cache frameworks.

Integration Method (Master)

MyBatis provides an cache interface that implements its own caching logic to implement cache interface development.

MyBatis and Ehcache integration, the MyBatis and Ehcache integration packages provide an implementation class for the cache interface.

 PackageOrg.apache.ibatis.cache;ImportJava.util.concurrent.locks.ReadWriteLock;/** * SPI for cache providers. * One instance of the cache would be created for each namespace. * * The cache Implement Ation must has a constructor that receives the cache ID as a String parameter. * * MyBatis would pass the namespace as ID to the constructor. * * <pre> * public Mycache (final String ID) {* if (id = = NULL) {* Throw new IllegalArgumentException ("Cache I Nstances 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 Resul   T of a SELECT. */  voidPutObject (Object key, object value);/** * @param Key The key * @return The object stored in the cache. */Object GetObject (object key);/** * Optional.   It isn't called by the core. * * @param Key The key * @return The object that is removed */Object Removeobject (object key);/** * Clears this cache instance */    voidClear ();/** * Optional.   This method isn't called by the core.   * * @return The number of elements stored in the the cache (not its capacity). */  intGetSize ();/** * Optional.   As of 3.2.6 This method was no longer called by the core.   * * Any locking needed by the cache must is provided internally by the cache provider. * * @return A readwritelock * *Readwritelock Getreadwritelock ();}

The MyBatis default implementation of the Cache class is:

 PackageOrg.apache.ibatis.cache.impl;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.concurrent.locks.ReadWriteLock;ImportOrg.apache.ibatis.cache.Cache;ImportOrg.apache.ibatis.cache.CacheException;/** * @author Clinton Begin * * Public  class Perpetualcache implements Cache {  PrivateString ID;PrivateMap<object, object> cache =NewHashmap<object, object> (); Public Perpetualcache(String ID) { This. id = ID; } PublicStringgetId() {returnId } Public int GetSize() {returnCache.size (); } Public void PutObject(Object key, Object value)  {Cache.put (key, value); } PublicObjectGetObject(Object key) {returnCache.get (key); } PublicObjectRemoveobject(Object key) {returnCache.remove (key); } Public void Clear() {cache.clear (); } PublicReadwritelockGetreadwritelock() {return NULL; } Public Boolean equals(Object o) {if(getId () = =NULL)Throw NewCacheexception ("Cache instances require an ID.");if( This= = O)return true;if(! (OinstanceofCache))return false; Cache Othercache = (cache) O;returnGetId (). Equals (Othercache.getid ()); } Public int hashcode() {if(getId () = =NULL)Throw NewCacheexception ("Cache instances require an ID.");returnGetId (). Hashcode (); }}
Integrated Ehcache
    • Add Ehcache Pack
      • Ehcache-core-2.6.5.jar
      • Mybatis-ehcache-1.0.2.jar

Configuring the cache type implementation type for the Ehcache cache interface in Mapper

<!-- 开启本mapper的namespace下的二级缓存    type:指定cache接口的实现类的类型,mybatis默认使用PerpetualCache    要和ehcache整合,需要配置type为ehcache实现cache接口的类型    <cache />    -->    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
Add the Ehcache configuration file

Configure the Ehcache.xml under Classpath

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi: noNamespaceSchemaLocation=". /config/ehcache.xsd ">        <diskstore path="F:\develop\ehcache" />    <defaultcachemaxelementsinmemory="maxelementsondisk" =" 10000000 "eternal=" false "overflowtodisk=" false "timetoidleseconds ="timetoliveseconds" = "diskexpirythreadintervalseconds " ="memorystoreevictionpolicy" = "LRU">                                                                         </defaultcache></ehcache>

Author @brianway More articles: personal website | CSDN | Oschina

MyBatis Study notes (-mybatis) integration Ehcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.