org.apache.ibatis.cache.Cache Java Examples

The following examples show how to use org.apache.ibatis.cache.Cache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: tk-mybatis   Author: godlike110   File: MapperTemplate.java    License: MIT License 6 votes vote down vote up
/**
 * 检查是否配置过缓存
 *
 * @param ms
 * @throws Exception
 */
private void checkCache(MappedStatement ms) throws Exception {
    if (ms.getCache() == null) {
        String nameSpace = ms.getId().substring(0, ms.getId().lastIndexOf("."));
        Cache cache;
        try {
            //不存在的时候会抛出异常
            cache = ms.getConfiguration().getCache(nameSpace);
        } catch (IllegalArgumentException e) {
            return;
        }
        if (cache != null) {
            MetaObject metaObject = SystemMetaObject.forObject(ms);
            metaObject.setValue("cache", cache);
        }
    }
}
 
Example #2
Source Project: Shop-for-JavaWeb   Author: EleTeam   File: XMLMapperBuilder.java    License: MIT License 6 votes vote down vote up
private void cacheElement(XNode context) throws Exception {
	if (context != null) {
		String type = context.getStringAttribute("type", "PERPETUAL");
		Class<? extends Cache> typeClass = typeAliasRegistry
				.resolveAlias(type);
		String eviction = context.getStringAttribute("eviction", "LRU");
		Class<? extends Cache> evictionClass = typeAliasRegistry
				.resolveAlias(eviction);
		Long flushInterval = context.getLongAttribute("flushInterval");
		Integer size = context.getIntAttribute("size");
		boolean readWrite = !context.getBooleanAttribute("readOnly", false);
		Properties props = context.getChildrenAsProperties();
		builderAssistant.useNewCache(typeClass, evictionClass,
				flushInterval, size, readWrite, props);
	}
}
 
Example #3
Source Project: mybaties   Author: shurun19851206   File: PerpetualCache.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
  //只要id相等就认为两个cache相同
  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());
}
 
Example #4
Source Project: mybaties   Author: shurun19851206   File: MapperBuilderAssistant.java    License: Apache License 2.0 6 votes vote down vote up
public Cache useCacheRef(String namespace) {
  if (namespace == null) {
    throw new BuilderException("cache-ref element requires a namespace attribute.");
  }
  try {
    unresolvedCacheRef = true;
    Cache cache = configuration.getCache(namespace);
    if (cache == null) {
      throw new IncompleteElementException("No cache for namespace '" + namespace + "' could be found.");
    }
    currentCache = cache;
    unresolvedCacheRef = false;
    return cache;
  } catch (IllegalArgumentException e) {
    throw new IncompleteElementException("No cache for namespace '" + namespace + "' could be found.", e);
  }
}
 
Example #5
Source Project: mybaties   Author: shurun19851206   File: MapperBuilderAssistant.java    License: Apache License 2.0 6 votes vote down vote up
public Cache useNewCache(Class<? extends Cache> typeClass,
    Class<? extends Cache> evictionClass,
    Long flushInterval,
    Integer size,
    boolean readWrite,
    boolean blocking,
    Properties props) {
    //这里面又判断了一下是否为null就用默认值,有点和XMLMapperBuilder.cacheElement逻辑重复了
  typeClass = valueOrDefault(typeClass, PerpetualCache.class);
  evictionClass = valueOrDefault(evictionClass, LruCache.class);
  //调用CacheBuilder构建cache,id=currentNamespace
  Cache cache = new CacheBuilder(currentNamespace)
      .implementation(typeClass)
      .addDecorator(evictionClass)
      .clearInterval(flushInterval)
      .size(size)
      .readWrite(readWrite)
      .blocking(blocking)
      .properties(props)
      .build();
  //加入缓存
  configuration.addCache(cache);
  //当前的缓存
  currentCache = cache;
  return cache;
}
 
Example #6
Source Project: mybaties   Author: shurun19851206   File: XMLMapperBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private void cacheElement(XNode context) throws Exception {
    if (context != null) {
      String type = context.getStringAttribute("type", "PERPETUAL");
      Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
      String eviction = context.getStringAttribute("eviction", "LRU");
      Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
      Long flushInterval = context.getLongAttribute("flushInterval");
      Integer size = context.getIntAttribute("size");
      boolean readWrite = !context.getBooleanAttribute("readOnly", false);
      boolean blocking = context.getBooleanAttribute("blocking", false);
      //读入额外的配置信息,易于第三方的缓存扩展,例:
//    <cache type="com.domain.something.MyCustomCache">
//      <property name="cacheFile" value="/tmp/my-custom-cache.tmp"/>
//    </cache>
      Properties props = context.getChildrenAsProperties();
      //调用builderAssistant.useNewCache
      builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
    }
  }
 
Example #7
Source Project: mybaties   Author: shurun19851206   File: CachingExecutor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
    throws SQLException {
  Cache cache = ms.getCache();
  //默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/>
  //简单的说,就是先查CacheKey,查不到再委托给实际的执行器去查
  if (cache != null) {
    flushCacheIfRequired(ms);
    if (ms.isUseCache() && resultHandler == null) {
      ensureNoOutParams(ms, parameterObject, boundSql);
      @SuppressWarnings("unchecked")
      List<E> list = (List<E>) tcm.getObject(cache, key);
      if (list == null) {
        list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
        tcm.putObject(cache, key, list); // issue #578 and #116
      }
      return list;
    }
  }
  return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
 
Example #8
Source Project: mybaties   Author: shurun19851206   File: MultipleCrossIncludeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example #9
Source Project: mybaties   Author: shurun19851206   File: XmlExternalRefTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example #10
Source Project: Mapper   Author: abel533   File: MapperCacheDisabler.java    License: MIT License 6 votes vote down vote up
private void removeStaticCache(Class<?> utilClass, String fieldName) {
    try {
        Field cacheField = ReflectionUtils.findField(utilClass, fieldName);
        if (cacheField != null) {
            ReflectionUtils.makeAccessible(cacheField);
            Object cache = ReflectionUtils.getField(cacheField, null);
            if (cache instanceof Map) {
                ((Map) cache).clear();
            } else if (cache instanceof Cache) {
                ((Cache) cache).clear();
            } else {
                throw new UnsupportedOperationException("cache field must be a java.util.Map " +
                        "or org.apache.ibatis.cache.Cache instance");
            }
            logger.info("Clear " + utilClass.getCanonicalName() + " " + fieldName + " cache.");
        }
    } catch (Exception ex) {
        logger.warn("Failed to disable " + utilClass.getCanonicalName() + " "
                + fieldName + " cache. ClassCastExceptions may occur", ex);
    }
}
 
Example #11
Source Project: mybatis   Author: tuguangquan   File: PerpetualCache.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
  //只要id相等就认为两个cache相同
  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());
}
 
Example #12
Source Project: mybatis   Author: tuguangquan   File: MapperBuilderAssistant.java    License: Apache License 2.0 6 votes vote down vote up
public Cache useCacheRef(String namespace) {
  if (namespace == null) {
    throw new BuilderException("cache-ref element requires a namespace attribute.");
  }
  try {
    unresolvedCacheRef = true;
    Cache cache = configuration.getCache(namespace);
    if (cache == null) {
      throw new IncompleteElementException("No cache for namespace '" + namespace + "' could be found.");
    }
    currentCache = cache;
    unresolvedCacheRef = false;
    return cache;
  } catch (IllegalArgumentException e) {
    throw new IncompleteElementException("No cache for namespace '" + namespace + "' could be found.", e);
  }
}
 
Example #13
Source Project: mybatis   Author: tuguangquan   File: MapperBuilderAssistant.java    License: Apache License 2.0 6 votes vote down vote up
public Cache useNewCache(Class<? extends Cache> typeClass,
    Class<? extends Cache> evictionClass,
    Long flushInterval,
    Integer size,
    boolean readWrite,
    boolean blocking,
    Properties props) {
    //这里面又判断了一下是否为null就用默认值,有点和XMLMapperBuilder.cacheElement逻辑重复了
  typeClass = valueOrDefault(typeClass, PerpetualCache.class);
  evictionClass = valueOrDefault(evictionClass, LruCache.class);
  //调用CacheBuilder构建cache,id=currentNamespace
  Cache cache = new CacheBuilder(currentNamespace)
      .implementation(typeClass)
      .addDecorator(evictionClass)
      .clearInterval(flushInterval)
      .size(size)
      .readWrite(readWrite)
      .blocking(blocking)
      .properties(props)
      .build();
  //加入缓存
  configuration.addCache(cache);
  //当前的缓存
  currentCache = cache;
  return cache;
}
 
Example #14
Source Project: mybatis   Author: tuguangquan   File: XMLMapperBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private void cacheElement(XNode context) throws Exception {
    if (context != null) {
      String type = context.getStringAttribute("type", "PERPETUAL");
      Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
      String eviction = context.getStringAttribute("eviction", "LRU");
      Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
      Long flushInterval = context.getLongAttribute("flushInterval");
      Integer size = context.getIntAttribute("size");
      boolean readWrite = !context.getBooleanAttribute("readOnly", false);
      boolean blocking = context.getBooleanAttribute("blocking", false);
      //读入额外的配置信息,易于第三方的缓存扩展,例:
//    <cache type="com.domain.something.MyCustomCache">
//      <property name="cacheFile" value="/tmp/my-custom-cache.tmp"/>
//    </cache>
      Properties props = context.getChildrenAsProperties();
      //调用builderAssistant.useNewCache
      builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
    }
  }
 
Example #15
Source Project: mybatis   Author: tuguangquan   File: CachingExecutor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
    throws SQLException {
  Cache cache = ms.getCache();
  //默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/>
  //简单的说,就是先查CacheKey,查不到再委托给实际的执行器去查
  if (cache != null) {
    flushCacheIfRequired(ms);
    if (ms.isUseCache() && resultHandler == null) {
      ensureNoOutParams(ms, parameterObject, boundSql);
      @SuppressWarnings("unchecked")
      List<E> list = (List<E>) tcm.getObject(cache, key);
      if (list == null) {
        list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
        tcm.putObject(cache, key, list); // issue #578 and #116
      }
      return list;
    }
  }
  return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
 
Example #16
Source Project: mybatis   Author: tuguangquan   File: MultipleCrossIncludeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example #17
Source Project: mybatis   Author: tuguangquan   File: XmlExternalRefTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example #18
Source Project: cacheonix-core   Author: cacheonix   File: MyBatisCache.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
private MyBatisCacheAdapter getSelectCacheAdapter(final String selectID) {

      lock.lock();
      try {

         final MyBatisCacheAdapter myBatisCacheAdapter = selectCacheMap.get(selectID);
         if (myBatisCacheAdapter == null) {

            if (cacheonix.cacheExists(selectID)) {

               return registerCacheAdapter(selectID, new MyBatisCacheAdapterImpl(cacheonix.getCache(selectID)));
            } else {

               if (StringUtils.isBlank(selectCacheTemplateName)) {

                  throw new IllegalArgumentException("Cacheonix could find configuration for cache '" + selectID + "' and property 'selectCacheTemplateName' is not set. Either configure the cache in cacheonix-config.xml or create a template.");
               } else {

                  final org.cacheonix.cache.Cache cache = cacheonix.createCache(selectID, selectCacheTemplateName);
                  return registerCacheAdapter(selectID, new MyBatisCacheAdapterImpl(cache));
               }
            }
         } else {

            // Returns existing
            return myBatisCacheAdapter;
         }
      } finally {

         lock.unlock();
      }
   }
 
Example #19
Source Project: mybaties   Author: shurun19851206   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      String name = (String) entry.getKey();
      String value = (String) entry.getValue();
      if (metaCache.hasSetter(name)) {
        Class<?> type = metaCache.getSetterType(name);
        //下面就是各种基本类型的判断了,味同嚼蜡但是又不得不写
        if (String.class == type) {
          metaCache.setValue(name, value);
        } else if (int.class == type
            || Integer.class == type) {
          metaCache.setValue(name, Integer.valueOf(value));
        } else if (long.class == type
            || Long.class == type) {
          metaCache.setValue(name, Long.valueOf(value));
        } else if (short.class == type
            || Short.class == type) {
          metaCache.setValue(name, Short.valueOf(value));
        } else if (byte.class == type
            || Byte.class == type) {
          metaCache.setValue(name, Byte.valueOf(value));
        } else if (float.class == type
            || Float.class == type) {
          metaCache.setValue(name, Float.valueOf(value));
        } else if (boolean.class == type
            || Boolean.class == type) {
          metaCache.setValue(name, Boolean.valueOf(value));
        } else if (double.class == type
            || Double.class == type) {
          metaCache.setValue(name, Double.valueOf(value));
        } else {
          throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
        }
      }
    }
  }
}
 
Example #20
Source Project: mybaties   Author: shurun19851206   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
  Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(id);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #21
Source Project: mybaties   Author: shurun19851206   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(String.class);
  } catch (Exception e) {
    throw new CacheException("Invalid base cache implementation (" + cacheClass + ").  " +
        "Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: " + e, e);
  }
}
 
Example #22
Source Project: mybaties   Author: shurun19851206   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {
  Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(base);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache decorator (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #23
Source Project: mybaties   Author: shurun19851206   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getCacheDecoratorConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(Cache.class);
  } catch (Exception e) {
    throw new CacheException("Invalid cache decorator (" + cacheClass + ").  " +
        "Cache decorators must have a constructor that takes a Cache instance as a parameter.  Cause: " + e, e);
  }
}
 
Example #24
Source Project: mybaties   Author: shurun19851206   File: TransactionalCache.java    License: Apache License 2.0 5 votes vote down vote up
public TransactionalCache(Cache delegate) {
  this.delegate = delegate;
  //默认commit时不清缓存
  this.clearOnCommit = false;
  this.entriesToAddOnCommit = new HashMap<Object, Object>();
  this.entriesMissedInCache = new HashSet<Object>();
}
 
Example #25
Source Project: mybaties   Author: shurun19851206   File: SoftCache.java    License: Apache License 2.0 5 votes vote down vote up
public SoftCache(Cache delegate) {
  this.delegate = delegate;
  //默认链表可以存256元素
  this.numberOfHardLinks = 256;
  this.hardLinksToAvoidGarbageCollection = new LinkedList<Object>();
  this.queueOfGarbageCollectedEntries = new ReferenceQueue<Object>();
}
 
Example #26
Source Project: mybaties   Author: shurun19851206   File: MapperBuilderAssistant.java    License: Apache License 2.0 5 votes vote down vote up
private void setStatementCache(
    boolean isSelect,
    boolean flushCache,
    boolean useCache,
    Cache cache,
    MappedStatement.Builder statementBuilder) {
  flushCache = valueOrDefault(flushCache, !isSelect);
  useCache = valueOrDefault(useCache, isSelect);
  statementBuilder.flushCacheRequired(flushCache);
  statementBuilder.useCache(useCache);
  statementBuilder.cache(cache);
}
 
Example #27
Source Project: mybatis   Author: tuguangquan   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      String name = (String) entry.getKey();
      String value = (String) entry.getValue();
      if (metaCache.hasSetter(name)) {
        Class<?> type = metaCache.getSetterType(name);
        //下面就是各种基本类型的判断了,味同嚼蜡但是又不得不写
        if (String.class == type) {
          metaCache.setValue(name, value);
        } else if (int.class == type
            || Integer.class == type) {
          metaCache.setValue(name, Integer.valueOf(value));
        } else if (long.class == type
            || Long.class == type) {
          metaCache.setValue(name, Long.valueOf(value));
        } else if (short.class == type
            || Short.class == type) {
          metaCache.setValue(name, Short.valueOf(value));
        } else if (byte.class == type
            || Byte.class == type) {
          metaCache.setValue(name, Byte.valueOf(value));
        } else if (float.class == type
            || Float.class == type) {
          metaCache.setValue(name, Float.valueOf(value));
        } else if (boolean.class == type
            || Boolean.class == type) {
          metaCache.setValue(name, Boolean.valueOf(value));
        } else if (double.class == type
            || Double.class == type) {
          metaCache.setValue(name, Double.valueOf(value));
        } else {
          throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
        }
      }
    }
  }
}
 
Example #28
Source Project: mybatis   Author: tuguangquan   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
  Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(id);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #29
Source Project: mybatis   Author: tuguangquan   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(String.class);
  } catch (Exception e) {
    throw new CacheException("Invalid base cache implementation (" + cacheClass + ").  " +
        "Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: " + e, e);
  }
}
 
Example #30
Source Project: mybatis   Author: tuguangquan   File: CacheBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {
  Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(base);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache decorator (" + cacheClass + "). Cause: " + e, e);
  }
}