mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor: auto delete db memory cache
This commit is contained in:
parent
a93220f9d2
commit
712f0a8256
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 4,
|
"manifest_version": 4,
|
||||||
"type": "extension",
|
"type": "extension",
|
||||||
"name": "LLOneBot v3.14.0",
|
"name": "LLOneBot v3.14.1",
|
||||||
"slug": "LLOneBot",
|
"slug": "LLOneBot",
|
||||||
"description": "LiteLoaderQQNT的OneBotApi",
|
"description": "LiteLoaderQQNT的OneBotApi",
|
||||||
"version": "3.14.0",
|
"version": "3.14.1",
|
||||||
"icon": "./icon.jpg",
|
"icon": "./icon.jpg",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
@ -48,9 +48,23 @@ class DBUtil {
|
|||||||
}
|
}
|
||||||
initDB();
|
initDB();
|
||||||
}).then()
|
}).then()
|
||||||
|
|
||||||
|
const expiredMilliSecond = 1000 * 60 * 60;
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
this.cache = {}
|
// this.cache = {}
|
||||||
}, 1000 * 60 * 10)
|
// 清理时间较久的缓存
|
||||||
|
const now = Date.now()
|
||||||
|
for (let key in this.cache) {
|
||||||
|
let message: RawMessage = this.cache[key] as RawMessage;
|
||||||
|
if (message?.msgTime){
|
||||||
|
if ((now - (parseInt(message.msgTime) * 1000)) > expiredMilliSecond) {
|
||||||
|
delete this.cache[key]
|
||||||
|
// log("clear cache", key, message.msgTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, expiredMilliSecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
private addCache(msg: RawMessage) {
|
private addCache(msg: RawMessage) {
|
||||||
@ -60,15 +74,24 @@ class DBUtil {
|
|||||||
this.cache[longIdKey] = this.cache[shortIdKey] = msg
|
this.cache[longIdKey] = this.cache[shortIdKey] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public clearCache() {
|
||||||
|
this.cache = {}
|
||||||
|
}
|
||||||
|
|
||||||
async getMsgByShortId(shortMsgId: number): Promise<RawMessage> {
|
async getMsgByShortId(shortMsgId: number): Promise<RawMessage> {
|
||||||
const shortMsgIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + shortMsgId;
|
const shortMsgIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + shortMsgId;
|
||||||
if (this.cache[shortMsgIdKey]) {
|
if (this.cache[shortMsgIdKey]) {
|
||||||
|
// log("getMsgByShortId cache", shortMsgIdKey, this.cache[shortMsgIdKey])
|
||||||
return this.cache[shortMsgIdKey] as RawMessage
|
return this.cache[shortMsgIdKey] as RawMessage
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
const longId = await this.db.get(shortMsgIdKey);
|
const longId = await this.db.get(shortMsgIdKey);
|
||||||
const msg = await this.getMsgByLongId(longId)
|
const msg = await this.getMsgByLongId(longId)
|
||||||
this.addCache(msg)
|
this.addCache(msg)
|
||||||
return msg
|
return msg
|
||||||
|
} catch (e) {
|
||||||
|
log("getMsgByShortId db error", e.stack.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMsgByLongId(longId: string): Promise<RawMessage> {
|
async getMsgByLongId(longId: string): Promise<RawMessage> {
|
||||||
@ -76,10 +99,14 @@ class DBUtil {
|
|||||||
if (this.cache[longIdKey]) {
|
if (this.cache[longIdKey]) {
|
||||||
return this.cache[longIdKey] as RawMessage
|
return this.cache[longIdKey] as RawMessage
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
const data = await this.db.get(longIdKey)
|
const data = await this.db.get(longIdKey)
|
||||||
const msg = JSON.parse(data)
|
const msg = JSON.parse(data)
|
||||||
this.addCache(msg)
|
this.addCache(msg)
|
||||||
return msg
|
return msg
|
||||||
|
} catch (e) {
|
||||||
|
// log("getMsgByLongId db error", e.stack.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMsgBySeqId(seqId: string): Promise<RawMessage> {
|
async getMsgBySeqId(seqId: string): Promise<RawMessage> {
|
||||||
@ -87,10 +114,14 @@ class DBUtil {
|
|||||||
if (this.cache[seqIdKey]) {
|
if (this.cache[seqIdKey]) {
|
||||||
return this.cache[seqIdKey] as RawMessage
|
return this.cache[seqIdKey] as RawMessage
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
const longId = await this.db.get(seqIdKey);
|
const longId = await this.db.get(seqIdKey);
|
||||||
const msg = await this.getMsgByLongId(longId)
|
const msg = await this.getMsgByLongId(longId)
|
||||||
this.addCache(msg)
|
this.addCache(msg)
|
||||||
return msg
|
return msg
|
||||||
|
} catch (e) {
|
||||||
|
log("getMsgBySeqId db error", e.stack.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addMsg(msg: RawMessage) {
|
async addMsg(msg: RawMessage) {
|
||||||
@ -111,28 +142,25 @@ class DBUtil {
|
|||||||
return existMsg.msgShortId
|
return existMsg.msgShortId
|
||||||
}
|
}
|
||||||
this.addCache(msg);
|
this.addCache(msg);
|
||||||
// log("新增消息记录", msg.msgId)
|
|
||||||
const shortMsgId = await this.genMsgShortId();
|
const shortMsgId = await this.genMsgShortId();
|
||||||
const shortIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + shortMsgId;
|
const shortIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + shortMsgId;
|
||||||
const seqIdKey = this.DB_KEY_PREFIX_MSG_SEQ_ID + msg.msgSeq;
|
const seqIdKey = this.DB_KEY_PREFIX_MSG_SEQ_ID + msg.msgSeq;
|
||||||
msg.msgShortId = shortMsgId;
|
msg.msgShortId = shortMsgId;
|
||||||
try {
|
// log("新增消息记录", msg.msgId)
|
||||||
this.db.put(shortIdKey, msg.msgId).then();
|
this.db.put(shortIdKey, msg.msgId).then().catch();
|
||||||
this.db.put(longIdKey, JSON.stringify(msg)).then();
|
this.db.put(longIdKey, JSON.stringify(msg)).then().catch();
|
||||||
try {
|
try {
|
||||||
await this.db.get(seqIdKey)
|
await this.db.get(seqIdKey)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// log("新的seqId", seqIdKey)
|
// log("新的seqId", seqIdKey)
|
||||||
this.db.put(seqIdKey, msg.msgId).then();
|
this.db.put(seqIdKey, msg.msgId).then().catch();
|
||||||
}
|
}
|
||||||
if (!this.cache[seqIdKey]) {
|
if (!this.cache[seqIdKey]) {
|
||||||
this.cache[seqIdKey] = msg;
|
this.cache[seqIdKey] = msg;
|
||||||
}
|
}
|
||||||
// log(`消息入库 ${seqIdKey}: ${msg.msgId}, ${shortMsgId}: ${msg.msgId}`);
|
|
||||||
} catch (e) {
|
|
||||||
// log("addMsg db error", e.stack.toString());
|
|
||||||
}
|
|
||||||
return shortMsgId
|
return shortMsgId
|
||||||
|
// log(`消息入库 ${seqIdKey}: ${msg.msgId}, ${shortMsgId}: ${msg.msgId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateMsg(msg: RawMessage) {
|
async updateMsg(msg: RawMessage) {
|
||||||
@ -147,17 +175,17 @@ class DBUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(existMsg, msg)
|
Object.assign(existMsg, msg)
|
||||||
this.db.put(longIdKey, JSON.stringify(existMsg)).then();
|
this.db.put(longIdKey, JSON.stringify(existMsg)).then().catch();
|
||||||
const shortIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + existMsg.msgShortId;
|
const shortIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + existMsg.msgShortId;
|
||||||
const seqIdKey = this.DB_KEY_PREFIX_MSG_SEQ_ID + msg.msgSeq;
|
const seqIdKey = this.DB_KEY_PREFIX_MSG_SEQ_ID + msg.msgSeq;
|
||||||
if (!this.cache[seqIdKey]) {
|
if (!this.cache[seqIdKey]) {
|
||||||
this.cache[seqIdKey] = existMsg;
|
this.cache[seqIdKey] = existMsg;
|
||||||
}
|
}
|
||||||
this.db.put(shortIdKey, msg.msgId).then();
|
this.db.put(shortIdKey, msg.msgId).then().catch();
|
||||||
try {
|
try {
|
||||||
await this.db.get(seqIdKey)
|
await this.db.get(seqIdKey)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.db.put(seqIdKey, msg.msgId).then();
|
this.db.put(seqIdKey, msg.msgId).then().catch();
|
||||||
// log("更新seqId error", e.stack, seqIdKey);
|
// log("更新seqId error", e.stack, seqIdKey);
|
||||||
}
|
}
|
||||||
// log("更新消息", existMsg.msgSeq, existMsg.msgShortId, existMsg.msgId);
|
// log("更新消息", existMsg.msgSeq, existMsg.msgShortId, existMsg.msgId);
|
||||||
@ -176,7 +204,7 @@ class DBUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.currentShortId++;
|
this.currentShortId++;
|
||||||
await this.db.put(key, this.currentShortId.toString());
|
this.db.put(key, this.currentShortId.toString()).then().catch();
|
||||||
return this.currentShortId;
|
return this.currentShortId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +216,11 @@ class DBUtil {
|
|||||||
let cacheDBData = {...data}
|
let cacheDBData = {...data}
|
||||||
delete cacheDBData['downloadFunc']
|
delete cacheDBData['downloadFunc']
|
||||||
this.cache[fileName] = data;
|
this.cache[fileName] = data;
|
||||||
|
try {
|
||||||
await this.db.put(key, JSON.stringify(cacheDBData));
|
await this.db.put(key, JSON.stringify(cacheDBData));
|
||||||
|
} catch (e) {
|
||||||
|
log("addFileCache db error", e.stack.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFileCache(fileName: string): Promise<FileCache | undefined> {
|
async getFileCache(fileName: string): Promise<FileCache | undefined> {
|
||||||
@ -197,11 +229,10 @@ class DBUtil {
|
|||||||
return this.cache[key] as FileCache
|
return this.cache[key] as FileCache
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
||||||
let data = await this.db.get(key);
|
let data = await this.db.get(key);
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// log("getFileCache db error", e.stack.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,7 +243,7 @@ class DBUtil {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.cache[key] = notify;
|
this.cache[key] = notify;
|
||||||
this.db.put(key, JSON.stringify(notify)).then();
|
this.db.put(key, JSON.stringify(notify)).then().catch();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupNotify(seq: string): Promise<GroupNotify | undefined> {
|
async getGroupNotify(seq: string): Promise<GroupNotify | undefined> {
|
||||||
@ -224,7 +255,7 @@ class DBUtil {
|
|||||||
let data = await this.db.get(key);
|
let data = await this.db.get(key);
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// log("getGroupNotify db error", e.stack.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,6 +180,7 @@ async function processGroupEvent(payload) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 群列表变动
|
||||||
registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS, (payload) => {
|
registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS, (payload) => {
|
||||||
if (payload.updateType != 2) {
|
if (payload.updateType != 2) {
|
||||||
updateGroups(payload.groupList).then();
|
updateGroups(payload.groupList).then();
|
||||||
@ -199,6 +200,7 @@ registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUP
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 好友列表变动
|
||||||
registerReceiveHook<{
|
registerReceiveHook<{
|
||||||
data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: User[] }[]
|
data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: User[] }[]
|
||||||
}>(ReceiveCmd.FRIENDS, payload => {
|
}>(ReceiveCmd.FRIENDS, payload => {
|
||||||
@ -215,6 +217,7 @@ registerReceiveHook<{
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 新消息
|
||||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||||
const {autoDeleteFile} = getConfigUtil().getConfig();
|
const {autoDeleteFile} = getConfigUtil().getConfig();
|
||||||
for (const message of payload.msgList) {
|
for (const message of payload.msgList) {
|
||||||
@ -232,7 +235,9 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const picPath = msgElement.picElement?.sourcePath
|
const picPath = msgElement.picElement?.sourcePath
|
||||||
const pttPath = msgElement.pttElement?.filePath
|
const pttPath = msgElement.pttElement?.filePath
|
||||||
const pathList = [picPath, pttPath]
|
const filePath = msgElement.fileElement?.filePath
|
||||||
|
const videoPath = msgElement.videoElement?.filePath
|
||||||
|
const pathList = [picPath, pttPath, filePath, videoPath]
|
||||||
if (msgElement.picElement) {
|
if (msgElement.picElement) {
|
||||||
pathList.push(...Object.values(msgElement.picElement.thumbPath))
|
pathList.push(...Object.values(msgElement.picElement.thumbPath))
|
||||||
}
|
}
|
||||||
@ -249,7 +254,7 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 60 * 1000)
|
}, getConfigUtil().getConfig().autoDeleteFileSecond * 1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
ChatCacheListItemBasic,
|
ChatCacheListItemBasic,
|
||||||
CacheFileType
|
CacheFileType
|
||||||
} from '../../ntqqapi/types';
|
} from '../../ntqqapi/types';
|
||||||
|
import {dbUtil} from "../../common/db";
|
||||||
|
|
||||||
export default class CleanCache extends BaseAction<void, void> {
|
export default class CleanCache extends BaseAction<void, void> {
|
||||||
actionName = ActionName.CleanCache
|
actionName = ActionName.CleanCache
|
||||||
@ -15,6 +16,7 @@ export default class CleanCache extends BaseAction<void, void> {
|
|||||||
protected _handle(): Promise<void> {
|
protected _handle(): Promise<void> {
|
||||||
return new Promise<void>(async (res, rej) => {
|
return new Promise<void>(async (res, rej) => {
|
||||||
try {
|
try {
|
||||||
|
// dbUtil.clearCache();
|
||||||
const cacheFilePaths: string[] = [];
|
const cacheFilePaths: string[] = [];
|
||||||
|
|
||||||
await NTQQApi.setCacheSilentScan(false);
|
await NTQQApi.setCacheSilentScan(false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user