Skip to content

Commit 256dd81

Browse files
committed
优化缓存逻辑;优化 SQLConfig.getSQL(boolean prepared) 相关代码
1 parent f82576d commit 256dd81

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

APIJSONORM/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.Tencent</groupId>
77
<artifactId>APIJSON</artifactId>
8-
<version>5.5.0</version>
8+
<version>6.0.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONORM</name>

APIJSONORM/src/main/java/apijson/Log.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Log {
1414

1515
public static boolean DEBUG = true;
1616

17-
public static final String VERSION = "5.4.0";
17+
public static final String VERSION = "6.0.0";
1818
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
1919

2020
public static final String OS_NAME;

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,7 +4151,14 @@ public String getRemoveString(String key, String column, Object value) throws Il
41514151
@JSONField(serialize = false)
41524152
@Override
41534153
public String getSQL(boolean prepared) throws Exception {
4154-
return getSQL(this.setPrepared(prepared));
4154+
boolean isPrepared = isPrepared();
4155+
if (isPrepared == prepared) {
4156+
return getSQL(this);
4157+
}
4158+
4159+
String sql = getSQL(this.setPrepared(prepared));
4160+
setPrepared(isPrepared);
4161+
return sql;
41554162
}
41564163
/**
41574164
* @param config

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public List<JSONObject> getCache(String sql, SQLConfig config) {
119119
@Override
120120
public JSONObject getCacheItem(String sql, int position, SQLConfig config) {
121121
List<JSONObject> list = getCache(sql, config);
122+
return getCacheItem(list, position, config);
123+
}
124+
125+
public JSONObject getCacheItem(List<JSONObject> list, int position, SQLConfig config) {
122126
// 只要 list 不为 null,则如果 list.get(position) == null,则返回 {} ,避免再次 SQL 查询
123127
if (list == null) {
124128
return null;
@@ -128,6 +132,10 @@ public JSONObject getCacheItem(String sql, int position, SQLConfig config) {
128132
return result != null ? result : new JSONObject();
129133
}
130134

135+
136+
137+
138+
131139
/**移除缓存
132140
* @param sql key
133141
* @param config
@@ -168,9 +176,7 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
168176
public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception {
169177
long executedSQLStartTime = System.currentTimeMillis();
170178

171-
boolean isPrepared = config.isPrepared();
172179
final String sql = config.getSQL(false);
173-
config.setPrepared(isPrepared);
174180

175181
if (StringUtil.isEmpty(sql, true)) {
176182
Log.e(TAG, "execute StringUtil.isEmpty(sql, true) >> return null;");
@@ -219,7 +225,8 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
219225
//导致后面 rs.getMetaData() 报错 Operation not allowed after ResultSet closed result.put("moreResults", statement.getMoreResults());
220226
}
221227
else {
222-
switch (config.getMethod()) {
228+
RequestMethod method = config.getMethod();
229+
switch (method) {
223230
case POST:
224231
case PUT:
225232
case DELETE:
@@ -250,17 +257,26 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws
250257
result.put(idKey + "[]", config.getIdIn());
251258
}
252259

260+
if (method == RequestMethod.PUT || method == RequestMethod.DELETE) {
261+
config.setMethod(RequestMethod.GET);
262+
removeCache(config.getSQL(false), config);
263+
config.setMethod(method);
264+
}
265+
253266
return result;
254267

255268
case GET:
256269
case GETS:
257270
case HEAD:
258271
case HEADS:
259-
result = isHead || isExplain ? null : getCacheItem(sql, position, config);
272+
List<JSONObject> cache = getCache(sql, config);
273+
result = getCacheItem(cache, position, config);
260274
Log.i(TAG, ">>> execute result = getCache('" + sql + "', " + position + ") = " + result);
261275
if (result != null) {
262-
cachedSQLCount ++;
263-
List<JSONObject> cache = getCache(sql, config);
276+
if (isExplain == false) {
277+
cachedSQLCount ++;
278+
}
279+
264280
if (cache != null && cache.size() > 1) {
265281
result.put(KEY_RAW_LIST, cache);
266282
}
@@ -600,9 +616,13 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
600616
config.setExplain(false);
601617
result.put("sql", config.getSQL(false));
602618
config.setExplain(isExplain);
603-
config.setPrepared(isPrepared);
604619
}
605620
result.put("list", resultList);
621+
622+
if (unknownType == false) {
623+
putCache(sql, Arrays.asList(result), config);
624+
}
625+
606626
return result;
607627
}
608628

@@ -622,7 +642,6 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
622642
putCache(entry.getKey(), entry.getValue(), null);
623643
}
624644

625-
putCache(sql, resultList, config);
626645
Log.i(TAG, ">>> execute putCache('" + sql + "', resultList); resultList.size() = " + resultList.size());
627646

628647
// 数组主表对象额外一次返回全部,方便 Parser 缓存来提高性能
@@ -638,6 +657,8 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
638657
}
639658
}
640659

660+
putCache(sql, resultList, config);
661+
641662
long endTime = System.currentTimeMillis();
642663
Log.d(TAG, "\n\n execute endTime = " + endTime + "; duration = " + (endTime - startTime)
643664
+ "\n return resultList.get(" + position + ");" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
@@ -727,7 +748,6 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
727748

728749
boolean prepared = jc.isPrepared();
729750
String sql = jc.getSQL(false);
730-
jc.setPrepared(prepared);
731751

732752
if (StringUtil.isEmpty(sql, true)) {
733753
throw new NullPointerException(TAG + ".executeAppJoin StringUtil.isEmpty(sql, true) >> return null;");

0 commit comments

Comments
 (0)