use of org.sagacity.sqltoy.cache.TranslateCacheManager in project sagacity-sqltoy by chenrenfei.
the class TranslateManager method getCache.
/**
* @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存
* @param sqlToyContext
* @param conn
* @param cacheName
* @param cacheType
* 一般为null,不为空时一般用于数据字典等同于dictType
* @return
* @throws Exception
*/
public HashMap<String, Object[]> getCache(final SqlToyContext sqlToyContext, Connection conn, String cacheName, String cacheType) throws Exception {
// 获取缓存翻译的管理器
TranslateCacheModel cacheModel = translateMap.get(cacheName);
if (cacheModel == null) {
// cacheName);
return null;
}
TranslateCacheManager manager = getTranslateCacheManager(cacheModel.getTranslateCacheManager());
HashMap<String, Object[]> result = manager.getCache(cacheName, cacheType);
if (result == null || result.isEmpty()) {
final Object[] args = StringUtil.isBlank(cacheType) ? null : new Object[] { cacheType };
// sql 查询模式
if (StringUtil.isNotBlank(cacheModel.getSql())) {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(cacheModel.getSql(), SqlType.search);
// 避免sql中有:name 参数名称
String realSql = SqlConfigParseUtils.processNamedParamsQuery(sqlToyConfig.getSql()).getSql();
String dataSourceName = cacheModel.getDataSource();
if (dataSourceName == null)
dataSourceName = sqlToyConfig.getDataSource();
// 设置默认数据库
if (null == conn && StringUtil.isBlank(dataSourceName))
dataSourceName = getDefaultDataSource();
List cacheResult = null;
// 缓存sql来源于不同数据库
if (sqlToyConfig.getDataSourceShardingStragety() != null || StringUtil.isNotBlank(dataSourceName)) {
DataSource dataBase = null;
if (StringUtil.isNotBlank(dataSourceName))
dataBase = sqlToyContext.getDataSource(dataSourceName);
else {
// 考虑存在分库策略,update 2017-12-8
dataBase = ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(cacheModel.getSql(), null, args), null);
}
cacheResult = (List) DataSourceUtils.processDataSource(sqlToyContext, dataBase, new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, dialect), args, null, conn, 0, -1, -1).getRows());
}
});
} else {
cacheResult = DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, DataSourceUtils.getDialect(DataSourceUtils.getDbType(conn))), args, null, conn, 0, -1, -1).getRows();
}
result = new HashMap<String, Object[]>();
int cacheIndex = cacheModel.getKeyIndex();
List row;
for (int i = 0, n = cacheResult.size(); i < n; i++) {
row = (List) cacheResult.get(i);
Object[] rowAry = new Object[row.size()];
for (int j = 0, t = rowAry.length; j < t; j++) {
rowAry[j] = row.get(j);
}
result.put(rowAry[cacheIndex].toString(), rowAry);
}
} else {
// 通过spring 调用具体的bean 方法获取数据,必须返回的是HashMap结果
try {
result = (HashMap<String, Object[]>) sqlToyContext.getServiceData(cacheModel.getService(), cacheModel.getServiceMethod(), args);
} catch (Exception e) {
e.printStackTrace();
logger.error("缓存翻译通过接口服务:{}.{} 返回的结果必须是HashMap<key, Object[]{key,name1,..,nameX}> 类型的数据格式!", cacheModel.getService(), cacheModel.getServiceMethod());
}
}
// 放入缓存
if (result != null && !result.isEmpty()) {
manager.put(cacheModel.getCacheConfig(), cacheName, cacheType, result);
}
}
return result;
}
Aggregations