mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-06-11 07:57:28 +00:00
fix: 添加缓存表定时清理任务,修复 cache_link_info 无限增长
- CacheManager 添加 cleanupExpiredCache() 方法删除过期缓存记录 - PostExecVerticle 注册每小时执行一次的定时清理任务 - 原实现只有读时惰性检查过期,过期记录永远不会被删除,长期运行后数据库持续膨胀
This commit is contained in:
@@ -62,6 +62,16 @@ public class PostExecVerticle extends AbstractVerticle {
|
|||||||
LOGGER.info("未找到 AppRun 接口的实现类");
|
LOGGER.info("未找到 AppRun 接口的实现类");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册定时清理过期缓存任务(每小时执行一次)
|
||||||
|
vertx.setPeriodic(3600_000, 3600_000, id -> {
|
||||||
|
try {
|
||||||
|
cn.qaiu.lz.common.cache.CacheManager cacheManager = new cn.qaiu.lz.common.cache.CacheManager();
|
||||||
|
cacheManager.cleanupExpiredCache();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("定时清理缓存任务跳过(数据库可能未就绪)", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
LOGGER.info("PostExecVerticle 执行完成");
|
LOGGER.info("PostExecVerticle 执行完成");
|
||||||
startPromise.complete();
|
startPromise.complete();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,6 +200,31 @@ public class CacheManager {
|
|||||||
return promise.future();
|
return promise.future();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理过期缓存记录,防止数据库无限增长
|
||||||
|
* @return 删除的行数
|
||||||
|
*/
|
||||||
|
public Future<Integer> cleanupExpiredCache() {
|
||||||
|
String sql = "DELETE FROM cache_link_info WHERE expiration > 0 AND expiration < #{now}";
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("now", System.currentTimeMillis());
|
||||||
|
Promise<Integer> promise = Promise.promise();
|
||||||
|
SqlTemplate.forUpdate(jdbcPool, sql)
|
||||||
|
.execute(params)
|
||||||
|
.onSuccess(res -> {
|
||||||
|
int deleted = res.rowCount();
|
||||||
|
if (deleted > 0) {
|
||||||
|
LOGGER.info("清理过期缓存记录 {} 条", deleted);
|
||||||
|
}
|
||||||
|
promise.complete(deleted);
|
||||||
|
})
|
||||||
|
.onFailure(e -> {
|
||||||
|
LOGGER.error("清理过期缓存失败", e);
|
||||||
|
promise.fail(e);
|
||||||
|
});
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
public Future<Map<String, Integer>> getShareKeyTotal(String shareKey) {
|
public Future<Map<String, Integer>> getShareKeyTotal(String shareKey) {
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT `share_key`, SUM(cache_hit_total) AS hit_total, SUM(api_parser_total) AS parser_total
|
SELECT `share_key`, SUM(cache_hit_total) AS hit_total, SUM(api_parser_total) AS parser_total
|
||||||
|
|||||||
Reference in New Issue
Block a user