Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.sylinx.hbatis.db.common.DefaultCacheQuery Maven / Gradle / Ivy
package cn.sylinx.hbatis.db.common;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.sylinx.hbatis.db.cache.CacheKeyGenerator;
import cn.sylinx.hbatis.db.cache.CacheQuery;
import cn.sylinx.hbatis.db.cache.ICacheKit;
import cn.sylinx.hbatis.db.cache.IDataLoader;
import cn.sylinx.hbatis.db.mapper.ModelBuilder;
import cn.sylinx.hbatis.db.mapper.QueryMapper;
import cn.sylinx.hbatis.exception.HbatisException;
import cn.sylinx.hbatis.ext.parse.SqlParser;
import cn.sylinx.hbatis.ext.res.ClasspathSqlResource;
import cn.sylinx.hbatis.kit.StrKit;
import cn.sylinx.hbatis.kit.Tuple;
import cn.sylinx.hbatis.log.GLog;
/**
* 缓存查询实现类
*
* @author han
*
*/
public class DefaultCacheQuery implements CacheQuery {
/**
* DbPro对象
*/
private final DbPro dbPro;
private ICacheKit cacheKit;
private Map cachedKey = new HashMap<>();
public DefaultCacheQuery(DbPro dbPro) {
this.dbPro = dbPro;
this.cacheKit = ICacheKit.create();
}
public String getSqlMD5(String key) {
if (cachedKey.containsKey(key)) {
return cachedKey.get(key);
}
if (StrKit.isEmpty(key)) {
return "";
}
MessageDigest digest = null;
byte buffer[] = key.getBytes(Charset.defaultCharset());
try {
digest = MessageDigest.getInstance("MD5");
digest.update(buffer);
} catch (Exception e) {
GLog.error("md5 encode error", e);
throw new HbatisException(e);
}
BigInteger bigInt = new BigInteger(1, digest.digest());
String ecodeStr = bigInt.toString(16);
cachedKey.put(key, ecodeStr);
return ecodeStr;
}
@Override
public Record queryFirstRecord(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryFirstRecord(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q1", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q1]: 'public Record queryFirstRecord(final String sql, final Object... params)' , cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public Record load() {
return dbPro.queryFirstRecord(sql, params);
}
});
}
@Override
public List queryRecords(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryRecords(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q2", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q2]: 'public List queryRecords(final String sql, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public List load() {
return dbPro.queryRecords(sql, params);
}
});
}
@Override
public List query(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.query(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q3", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q3]: 'public List query(final String sql, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public List load() {
return dbPro.query(sql, params);
}
});
}
@Override
public List> queryMap(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryMap(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q4", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q4]: 'public List> queryMap(final String sql, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public List> load() {
return dbPro.queryMap(sql, params);
}
});
}
@Override
public Object[] queryFirst(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryFirst(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q5", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q5]: 'public Object[] queryFirst(final String sql, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public Object[] load() {
return dbPro.queryFirst(sql, params);
}
});
}
@Override
public Map queryFirstMap(final String sql, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryFirstMap(sql, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q6", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q6]: 'public Map queryFirstMap(final String sql, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public Map load() {
return dbPro.queryFirstMap(sql, params);
}
});
}
@Override
public List query(final String sql, final QueryMapper mapper, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.query(sql, mapper, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q7", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q7]: 'public List query(final String sql, final QueryMapper mapper, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public List load() {
return dbPro.query(sql, mapper, params);
}
});
}
@Override
public T queryFirst(final String sql, final QueryMapper mapper, final Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryFirst(sql, mapper, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q8", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q8]: 'public T queryFirst(final String sql, final QueryMapper mapper, final Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public T load() {
return dbPro.queryFirst(sql, mapper, params);
}
});
}
@Override
public List queryObject(String sql, Class clazz, Object... params) {
if (!cacheKit.isInited()) {
GLog.debug("no cache config, query from db");
return dbPro.queryObject(sql, clazz, params);
}
Object key = CacheKeyGenerator.generateCacheKey(dbPro.getDataSourceName() + "Q9", sql, params);
String cacheKey = getSqlMD5(String.valueOf(key));
GLog.debug(
"use cache query[Q9]: 'public List queryObject(String sql, Class clazz, Object... params)', cache key:{}",
cacheKey);
return cacheKit.get(cacheKey, new IDataLoader() {
@Override
public List load() {
return dbPro.queryObject(sql, clazz, params);
}
});
}
@Override
public T queryFirstObject(String sql, Class clazz, Object... params) {
List dataList = queryObject(sql, clazz, params);
return dataList.isEmpty() ? null : dataList.get(0);
}
@Override
public List> queryForMapList(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryMap(st, pms);
}
@Override
public List queryForRecords(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryRecords(st, pms);
}
@Override
public Record queryFirstRecord(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryFirstRecord(st, pms);
}
@Override
public List queryObjectArrayList(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return query(st, pms);
}
@Override
public Object[] queryFirstObjectArray(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryFirst(st, pms);
}
@Override
public Map queryFirstForMap(ClasspathSqlResource sqlResource, Map params) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryFirstMap(st, pms);
}
@Override
public List query(ClasspathSqlResource sqlResource, Map params, Class clz) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
QueryMapper mapper = ModelBuilder.buildQueryMapper(clz);
return query(st, mapper, pms);
}
@Override
public T queryFirst(ClasspathSqlResource sqlResource, Map params, Class clz) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
QueryMapper mapper = ModelBuilder.buildQueryMapper(clz);
return queryFirst(st, mapper, pms);
}
@Override
public List queryObjectList(ClasspathSqlResource sqlResource, Map params, Class clz) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
return queryObject(st, clz, pms);
}
}