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 File: XmlExternalRefTest.java From mybaties with Apache License 2.0 | 6 votes |
@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 #2
Source File: PerpetualCache.java From mybatis with Apache License 2.0 | 6 votes |
@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 #3
Source File: MapperBuilderAssistant.java From mybatis with Apache License 2.0 | 6 votes |
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 #4
Source File: MapperBuilderAssistant.java From mybatis with Apache License 2.0 | 6 votes |
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 #5
Source File: PerpetualCache.java From mybaties with Apache License 2.0 | 6 votes |
@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 #6
Source File: XMLMapperBuilder.java From mybatis with Apache License 2.0 | 6 votes |
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 File: MapperBuilderAssistant.java From mybaties with Apache License 2.0 | 6 votes |
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 #8
Source File: MapperBuilderAssistant.java From mybaties with Apache License 2.0 | 6 votes |
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 #9
Source File: XMLMapperBuilder.java From mybaties with Apache License 2.0 | 6 votes |
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 #10
Source File: MapperCacheDisabler.java From Mapper with MIT License | 6 votes |
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 File: CachingExecutor.java From mybatis with Apache License 2.0 | 6 votes |
@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 #12
Source File: CachingExecutor.java From mybaties with Apache License 2.0 | 6 votes |
@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 #13
Source File: MapperTemplate.java From tk-mybatis with MIT License | 6 votes |
/** * 检查是否配置过缓存 * * @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 #14
Source File: XMLMapperBuilder.java From Shop-for-JavaWeb with MIT License | 6 votes |
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 #15
Source File: MultipleCrossIncludeTest.java From mybaties with Apache License 2.0 | 6 votes |
@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 #16
Source File: MultipleCrossIncludeTest.java From mybatis with Apache License 2.0 | 6 votes |
@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 File: XmlExternalRefTest.java From mybatis with Apache License 2.0 | 6 votes |
@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 File: CacheBuilder.java From mybaties with Apache License 2.0 | 5 votes |
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 #19
Source File: DatabaseAccessHelper.java From gocd with Apache License 2.0 | 5 votes |
public void onSetUp() throws Exception { databaseTester.onSetup(); pipelineTimeline.clearWhichIsEvilAndShouldNotBeUsedInRealWorld(); if (sqlMapClient != null) { for (Cache cache : sqlMapClient.getConfiguration().getCaches()) { cache.clear(); } } }
Example #20
Source File: MyBatisCache.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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 #21
Source File: CacheBuilder.java From mybaties with Apache License 2.0 | 5 votes |
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 #22
Source File: CacheBuilder.java From mybaties with Apache License 2.0 | 5 votes |
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 #23
Source File: CacheBuilder.java From mybatis with Apache License 2.0 | 5 votes |
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 #24
Source File: CacheBuilder.java From mybatis with Apache License 2.0 | 5 votes |
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 #25
Source File: CacheBuilder.java From mybatis with Apache License 2.0 | 5 votes |
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 #26
Source File: TransactionalCache.java From mybaties with Apache License 2.0 | 5 votes |
public TransactionalCache(Cache delegate) { this.delegate = delegate; //默认commit时不清缓存 this.clearOnCommit = false; this.entriesToAddOnCommit = new HashMap<Object, Object>(); this.entriesMissedInCache = new HashSet<Object>(); }
Example #27
Source File: CacheBuilder.java From mybatis with Apache License 2.0 | 5 votes |
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 File: TransactionalCache.java From mybatis with Apache License 2.0 | 5 votes |
public TransactionalCache(Cache delegate) { this.delegate = delegate; //默认commit时不清缓存 this.clearOnCommit = false; this.entriesToAddOnCommit = new HashMap<Object, Object>(); this.entriesMissedInCache = new HashSet<Object>(); }
Example #29
Source File: SoftCache.java From mybatis with Apache License 2.0 | 5 votes |
public SoftCache(Cache delegate) { this.delegate = delegate; //默认链表可以存256元素 this.numberOfHardLinks = 256; this.hardLinksToAvoidGarbageCollection = new LinkedList<Object>(); this.queueOfGarbageCollectedEntries = new ReferenceQueue<Object>(); }
Example #30
Source File: MapperBuilderAssistant.java From mybaties with Apache License 2.0 | 5 votes |
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); }