1. 缓存优化

This commit is contained in:
qaiu
2024-09-18 13:24:33 +08:00
parent 4516aa8300
commit 0bbf022c5d
24 changed files with 370 additions and 263 deletions

View File

@@ -29,9 +29,9 @@ public class CacheConfigLoader {
}
public static Integer getDuration(PanDomainTemplate pdt) {
return CONFIGS.get(pdt.getShortName());
return CONFIGS.get(pdt.name().toLowerCase());
}
public static Integer getDuration(String shortName) {
return CONFIGS.get(shortName);
public static Integer getDuration(String type) {
return CONFIGS.get(type.toLowerCase());
}
}

View File

@@ -6,6 +6,7 @@ import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.templates.SqlTemplate;
import java.util.HashMap;
@@ -16,7 +17,7 @@ public class CacheManager {
public Future<CacheLinkInfo> get(String cacheKey) {
String sql = "SELECT direct_link as directLink, expiration FROM cache_link_info WHERE share_key = #{share_key}";
String sql = "SELECT share_key as shareKey, direct_link as directLink, expiration FROM cache_link_info WHERE share_key = #{share_key}";
Map<String, Object> params = new HashMap<>();
params.put("share_key", cacheKey);
Promise<CacheLinkInfo> promise = Promise.promise();
@@ -29,7 +30,7 @@ public class CacheManager {
cacheHit = rows.iterator().next();
cacheHit.setCacheHit(true);
} else {
cacheHit = new CacheLinkInfo(JsonObject.of("cacheHit", false));
cacheHit = new CacheLinkInfo(JsonObject.of("cacheHit", false, "shareKey", cacheKey));
}
promise.complete(cacheHit);
}).onFailure(Throwable::printStackTrace);
@@ -49,4 +50,80 @@ public class CacheManager {
.execute(cacheLinkInfo)
.mapEmpty();
}
// 统计网盘厂商API解析次数
public Future<Integer> updateTotalByCached(String shareKey) {
Promise<Integer> promise = Promise.promise();
String sql = """
MERGE INTO `api_statistics_info` (`pan_type`, `share_key`, `cache_hit_total`, `update_ts`)
KEY (`share_key`)
VALUES (#{panType}, #{shareKey}, #{total}, #{ts})
""";
getShareKeyTotal(shareKey, "cache_hit_total").onSuccess(total -> {
Integer newTotal = (total == null ? 0 : total) + 1;
SqlTemplate.forUpdate(jdbcPool, sql)
.execute(new HashMap<>() {{
put("panType", getShareType(shareKey));
put("shareKey", shareKey);
put("total", newTotal);
put("ts", System.currentTimeMillis());
}})
.onSuccess(res -> promise.complete(res.rowCount()))
.onFailure(Throwable::printStackTrace);
});
return promise.future();
}
private String getShareType(String fullShareKey) {
// 将type和shareKey组合成一个字符串作为缓存key
return fullShareKey.split(":")[0];
}
// 统计网盘厂商API解析次数
public Future<Integer> updateTotalByParser(String shareKey) {
Promise<Integer> promise = Promise.promise();
String sql = """
MERGE INTO `api_statistics_info` (`pan_type`, `share_key`, `api_parser_total`, `update_ts`)
KEY (`share_key`)
VALUES (#{panType}, #{shareKey}, #{total}, #{ts})
""";
getShareKeyTotal(shareKey, "api_parser_total").onSuccess(total -> {
Integer newTotal = (total == null ? 0 : total) + 1;
SqlTemplate.forUpdate(jdbcPool, sql)
.execute(new HashMap<>() {{
put("panType", getShareType(shareKey));
put("shareKey", shareKey);
put("total", newTotal);
put("ts", System.currentTimeMillis());
}})
.onSuccess(res -> promise.complete(res.rowCount()))
.onFailure(Throwable::printStackTrace);
});
return promise.future();
}
public Future<Integer> getShareKeyTotal(String shareKey, String name) {
String sql = """
select `share_key`, sum({total_name}) sum_num
from `api_statistics_info`
group by `share_key` having `share_key` = #{shareKey};
""".replace("{total_name}", name);
Promise<Integer> promise = Promise.promise();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("shareKey", shareKey);
SqlTemplate.forQuery(jdbcPool, sql)
.mapTo(Row::toJson)
.execute(paramMap)
.onSuccess(res -> {
Integer total = res.iterator().hasNext() ?
res.iterator().next().getInteger("sum_num") : null;
promise.complete(total);
});
return promise.future();
}
}

View File

@@ -20,7 +20,7 @@ public class DefaultInterceptor implements BeforeInterceptor {
@Override
public void handle(RoutingContext ctx) {
System.out.println("进入前置拦截器1->" + ctx.request().path());
// System.out.println("进入前置拦截器1->" + ctx.request().path());
doNext(ctx);
}

View File

@@ -4,7 +4,6 @@ import cn.qaiu.db.pool.JDBCPoolInit;
import cn.qaiu.lz.common.model.ParserLogInfo;
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
import cn.qaiu.vx.core.interceptor.AfterInterceptor;
import cn.qaiu.vx.core.model.JsonResult;
import cn.qaiu.vx.core.util.CommonUtil;
import cn.qaiu.vx.core.util.SharedDataUtil;
import io.vertx.core.json.JsonArray;
@@ -36,21 +35,8 @@ public class LogStatistics implements AfterInterceptor {
ParserLogInfo parserLogInfo = new ParserLogInfo();
parserLogInfo.setPath(ctx.request().uri());
if (responseData == null) {
String location = ctx.response().headers().get("location");
if (location != null) {
parserLogInfo.setCode(200);
parserLogInfo.setData(location);
} else {
log.error("location不存在且responseData为空, path={}", ctx.request().path());
}
insert(parserLogInfo);
} else if (responseData.containsKey("code")) {
JsonResult<?> result = JsonResult.toJsonResult(responseData);
parserLogInfo.setCode(result.getCode());
parserLogInfo.setData(result.getCode() == 500 ? result.getMsg() : result.getData().toString());
insert(parserLogInfo);
if (responseData.containsKey("code") && responseData.getInteger("code") == 500) {
log.error("code 500: {} {}", ctx.request().path(), responseData.getString("msg"));
} else {
log.error("未知json日志: {}, path: {}", responseData.encode(), ctx.request().path());
}

View File

@@ -14,6 +14,8 @@ public class ParserLogInfo {
String id = SnowflakeIdWorker.getStringId();
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "GMT+8")
Date logTime = new Date();
@Length(varcharSize = 4096)
String path;
Integer code;