Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 评论回复的缓存逻辑优化,以及查看任务进行信息
牛文政1717d75cf093
diff --git a/electron/main/reply/cacheData.ts b/electron/main/reply/cacheData.tsnew file mode 100644index 00000000..044c62bc--- /dev/null+++ b/electron/main/reply/cacheData.ts@@ -0,0 +1,64 @@+import { GlobleCache } from '../../global/cache';++// 0 进行中 1 完成 2 错误+export enum AutorReplyCacheStatus {+ DOING = 0,+ DONE = 1,+ REEOR = 2,+}++export type AutoReplyCacheData = {+ status: AutorReplyCacheStatus;+ message: string;+ createTime?: number;+ updateTime?: number;+ title: string;+ dataId?: string;+};++export class AutoReplyCache {+ static cacheKey = 'OneKeyReplyCommentCacheKey';+ constructor(data: { title: string; dataId?: string }) {+ const cacheData = {+ status: AutorReplyCacheStatus.DOING,+ message: '进行中',+ updateTime: new Date().getTime(),+ createTime:+ (GlobleCache.getCache(AutoReplyCache.cacheKey) as AutoReplyCacheData)+ ?.createTime || new Date().getTime(),+ ...data,+ };++ GlobleCache.setCache(AutoReplyCache.cacheKey, cacheData, 60 * 15); // 设置缓存+ }++ // 获取信息+ static getInfo() {+ return GlobleCache.getCache(+ AutoReplyCache.cacheKey,+ ) as AutoReplyCacheData | null;+ }++ // 延长ttl+ extendTTL() {+ GlobleCache.updateCacheTTL(AutoReplyCache.cacheKey, 60 * 15); // 重设缓存时间+ }++ // 更新状态+ updateStatus(status: AutorReplyCacheStatus, message?: string) {+ const cacheData = GlobleCache.getCache(AutoReplyCache.cacheKey);+ if (cacheData) {+ GlobleCache.setCache(AutoReplyCache.cacheKey, {+ ...cacheData,+ status,+ message,+ updateTime: new Date().getTime(),+ });+ }+ }++ // 删除+ delete() {+ GlobleCache.delCache(AutoReplyCache.cacheKey);+ }+}diff --git a/electron/main/reply/controller.ts b/electron/main/reply/controller.tsindex 0133ab5a..8f756ae2 100644--- a/electron/main/reply/controller.ts+++ b/electron/main/reply/controller.ts@@ -16,6 +16,7 @@ import { SendChannelEnum } from '../../../commont/UtilsEnum';import { AutorReplyCommentScheduleEvent } from '../../../commont/types/reply';import type { WorkData } from '../plat/plat.type';import { GlobleCache } from '../../global/cache';+import { AutoReplyCache } from './cacheData';@Controller()export class ReplyController {@@ -351,4 +352,14 @@ export class ReplyController {return res.status === 1;}++ /**+ * 获取一键回复评论的任务信息+ */+ @Icp('ICP_GET_AUTO_REPLY_INFO')+ async getAutoReplyInfo(+ event: Electron.IpcMainInvokeEvent,+ ): Promise<any | null> {+ return AutoReplyCache.getInfo();+ }}diff --git a/electron/main/reply/service.ts b/electron/main/reply/service.tsindex 84f83bc7..106a2bc2 100644--- a/electron/main/reply/service.ts+++ b/electron/main/reply/service.ts@@ -19,17 +19,9 @@ import { AppDataSource } from '../../db';import { getUserInfo } from '../user/comment';import { AutoRunRecordStatus } from '../../db/models/autoRunRecord';import { WorkData } from '../plat/plat.type';-import { GlobleCache } from '../../global/cache';import { sleep } from '../../util/time';--/**- * 获取缓存key- * @param account- * @returns- */-function getCacheKey(account?: AccountModel) {- return `OneKeyReplyCommentCacheKey_${account?.id || 0}`;-}+import { AutoReplyCache, AutorReplyCacheStatus } from './cacheData';+import { logger } from '../../global/log';@Injectable()export class ReplyService {@@ -106,7 +98,11 @@ export class ReplyService {let theHasMore = true;let thePcursor = undefined;- GlobleCache.setCache(getCacheKey(account), data, 60 * 15); // 设置缓存+ // 设置缓存数据+ const cacheData = new AutoReplyCache({+ title: data.title || data.desc || '无',+ dataId: data.dataId,+ });try {scheduleEvent({@@ -115,7 +111,7 @@ export class ReplyService {});while (theHasMore) {- GlobleCache.updateCacheTTL(getCacheKey(account), 60 * 15); // 重设缓存时间+ cacheData.extendTTL(); // 延长缓存时间scheduleEvent({tag: AutorReplyCommentScheduleEvent.GetCommentListStart,@@ -133,7 +129,7 @@ export class ReplyService {tag: AutorReplyCommentScheduleEvent.End,status: 0,});- return true;+ break;}scheduleEvent({@@ -171,6 +167,10 @@ export class ReplyService {status: -1,error: '未获得AI产出内容',});+ cacheData.updateStatus(+ AutorReplyCacheStatus.REEOR,+ '未获得AI产出内容',+ );return false;}@@ -241,11 +241,13 @@ export class ReplyService {status: -1,error,});+ logger.error(['自动一键评论发生错误', error]);+ cacheData.updateStatus(AutorReplyCacheStatus.REEOR, '进行中发生错误');return false;}// 清除缓存- GlobleCache.delCache(getCacheKey(account));+ cacheData.delete();}/**@@ -262,16 +264,6 @@ export class ReplyService {status: 0 | 1;message?: string;}> {- // 查看缓存,有的就不执行- if (GlobleCache.getCache(getCacheKey(account))) {- sysNotice('请勿重复执行', `该账户有正在执行的任务,任务ID:${autoRun.id}`);-- return {- status: 0,- message: '该账户有正在执行的任务,请勿重复执行',- };- }-// 创建任务执行记录const recordData = await this.autoRunService.createAutoRunRecord(autoRun);diff --git a/src/icp/reply.ts b/src/icp/reply.tsindex bd54fdb0..598f87d8 100644--- a/src/icp/reply.ts+++ b/src/icp/reply.ts@@ -141,3 +141,24 @@ export async function ipcCreateAutoRunOfReply();return res;}++export enum AutorReplyCacheStatus {+ DOING = 0,+ DONE = 1,+ REEOR = 2,+}+/**+ * 获取一键回复的进程信息+ * @returns+ */+export async function ipcGetAutoRunOfReplyInfo() {+ const res: {+ status: AutorReplyCacheStatus;+ message: string;+ createTime?: number;+ updateTime?: number;+ title: string;+ dataId?: string;+ } = await window.ipcRenderer.invoke('ICP_GET_AUTO_REPLY_INFO');+ return res;+}