本文整理汇总了Java中org.apache.ibatis.cache.Cache类的典型用法代码示例。如果您正苦于以下问题:Java Cache类的具体用法?Java Cache怎么用?Java Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cache类属于org.apache.ibatis.cache包,在下文中一共展示了Cache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
/**
* 检查是否配置过缓存
*
* @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);
}
}
}
示例2: useCacheRef
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
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);
}
}
示例3: useNewCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
public Cache useNewCache(Class<? extends Cache> typeClass,
Class<? extends Cache> evictionClass,
Long flushInterval,
Integer size,
boolean readWrite,
boolean blocking,
Properties props) {
Cache cache = new CacheBuilder(currentNamespace)
.implementation(valueOrDefault(typeClass, PerpetualCache.class))
.addDecorator(valueOrDefault(evictionClass, LruCache.class))
.clearInterval(flushInterval)
.size(size)
.readWrite(readWrite)
.blocking(blocking)
.properties(props)
.build();
configuration.addCache(cache);
currentCache = cache;
return cache;
}
示例4: query
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
throws SQLException {
Cache cache = ms.getCache();
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);
}
示例5: testMappedStatementCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@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());
}
示例6: testMappedStatementCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@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());
}
示例7: cacheElement
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
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);
}
}
示例8: equals
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@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());
}
示例9: useNewCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
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;
}
示例10: cacheElement
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
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);
}
}
示例11: query
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@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);
}
示例12: equals
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Cache)) {
return false;
}
Cache otherCache = (Cache) obj;
return id.equals(otherCache.getId());
}
示例13: getCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@Override
public Cache getCache(String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
if (!cacheManager.cacheExists(id)) {
CacheConfiguration temp = null;
if (cacheConfiguration != null) {
temp = cacheConfiguration.clone();
} else {
// based on defaultCache
temp = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone();
}
temp.setName(id);
net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache(temp);
cacheManager.addCache(cache);
}
return new EhcacheCache(id, cacheManager.getEhcache(id));
}
示例14: getCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
@Override
public Cache getCache(String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
if (!cacheManager.cacheExists(id)) {
CacheConfiguration temp = null;
if (cacheConfiguration != null) {
temp = cacheConfiguration.clone();
} else {
// based on defaultCache
temp = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone();
}
temp.setName(id);
net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache(temp);
Ehcache instrumentCache = InstrumentedEhcache.instrument(registry, cache);
cacheManager.addCache(instrumentCache);
}
return new EhcacheCache(id, cacheManager.getEhcache(id));
}
示例15: useNewCache
import org.apache.ibatis.cache.Cache; //导入依赖的package包/类
public Cache useNewCache(Class<? extends Cache> typeClass,
Class<? extends Cache> evictionClass,
Long flushInterval,
Integer size,
boolean readWrite,
boolean blocking,
Properties props) {
typeClass = valueOrDefault(typeClass, PerpetualCache.class);
evictionClass = valueOrDefault(evictionClass, LruCache.class);
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;
}