Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 一键互动缓存逻辑优化
牛文政57d7eb8b0833
diff --git a/electron/main/interaction/cacheData.ts b/electron/main/interaction/cacheData.tsnew file mode 100644index 00000000..e710432d--- /dev/null+++ b/electron/main/interaction/cacheData.ts@@ -0,0 +1,67 @@+import { GlobleCache } from '../../global/cache';++// 0 进行中 1 完成 2 错误+export enum AutoInteractionCacheStatus {+ DOING = 0,+ DONE = 1,+ REEOR = 2,+}++export type AutoReplyCacheData = {+ status: AutoInteractionCacheStatus;+ message: string;+ createTime?: number;+ updateTime?: number;+ title: string;+ dataId?: string;+};++export class AutoInteractionCache {+ static cacheKey = 'OneKeyInteractionWorksCacheKey';+ constructor(data: { title: string }) {+ const cacheData = {+ status: AutoInteractionCacheStatus.DOING,+ message: '进行中',+ updateTime: new Date().getTime(),+ createTime:+ (+ GlobleCache.getCache(+ AutoInteractionCache.cacheKey,+ ) as AutoReplyCacheData+ )?.createTime || new Date().getTime(),+ ...data,+ };++ GlobleCache.setCache(AutoInteractionCache.cacheKey, cacheData, 60 * 30); // 设置缓存+ }++ // 获取信息+ static getInfo() {+ return GlobleCache.getCache(+ AutoInteractionCache.cacheKey,+ ) as AutoReplyCacheData | null;+ }++ // 延长ttl+ extendTTL() {+ GlobleCache.updateCacheTTL(AutoInteractionCache.cacheKey, 60 * 30); // 重设缓存时间+ }++ // 更新状态+ updateStatus(status: AutoInteractionCacheStatus, message?: string) {+ const cacheData = GlobleCache.getCache(AutoInteractionCache.cacheKey);+ if (cacheData) {+ GlobleCache.setCache(AutoInteractionCache.cacheKey, {+ ...cacheData,+ status,+ message,+ updateTime: new Date().getTime(),+ });+ }+ }++ // 删除+ delete() {+ GlobleCache.delCache(AutoInteractionCache.cacheKey);+ }+}diff --git a/electron/main/interaction/controller.ts b/electron/main/interaction/controller.tsindex 652e5473..1990d774 100644--- a/electron/main/interaction/controller.ts+++ b/electron/main/interaction/controller.ts@@ -14,6 +14,7 @@ import { InteractionService } from './service';import { SendChannelEnum } from '../../../commont/UtilsEnum';import type { WorkData } from '../plat/plat.type';import { AutorWorksInteractionScheduleEvent } from '../../../commont/types/interaction';+import { AutoInteractionCache } from './cacheData';@Controller()export class InteractionController {@@ -136,4 +137,14 @@ export class InteractionController {return res.status === 1;}++ /**+ * 获取一键互动的任务信息+ */+ @Icp('ICP_GET_AUTO_INTERACTION_INFO')+ async getAutoInteractionInfo(+ event: Electron.IpcMainInvokeEvent,+ ): Promise<any | null> {+ return AutoInteractionCache.getInfo();+ }}diff --git a/electron/main/interaction/service.ts b/electron/main/interaction/service.tsindex 537bb382..52ea53b9 100644--- a/electron/main/interaction/service.ts+++ b/electron/main/interaction/service.ts@@ -16,20 +16,11 @@ import { Repository } from 'typeorm';import { AppDataSource } from '../../db';import { getUserInfo } from '../user/comment';import { AutoRunRecordStatus } from '../../db/models/autoRunRecord';-import { GlobleCache } from '../../global/cache';import { sleep } from '../../util/time';import { InteractionRecordModel } from '../../db/models/interactionRecord';import { AutorWorksInteractionScheduleEvent } from '../../../commont/types/interaction';import { WorkData } from '../plat/plat.type';--/**- * 获取缓存key- * @param account- * @returns- */-function getCacheKey(account?: AccountModel) {- return `OneKeyInteractionWorksCacheKey_${account?.id || 0}`;-}+import { AutoInteractionCache } from './cacheData';@Injectable()export class InteractionService {@@ -106,8 +97,11 @@ export class InteractionService {}) => void,) {const userInfo = getUserInfo();+// 设置缓存- GlobleCache.setCache(getCacheKey(), worksList, 60 * 15);+ const cacheData = new AutoInteractionCache({+ title: '互动任务',+ });try {scheduleEvent({@@ -115,15 +109,7 @@ export class InteractionService {status: 0,});- // 重设缓存时间- GlobleCache.updateCacheTTL(getCacheKey(), 60 * 15);-- scheduleEvent({- tag: AutorWorksInteractionScheduleEvent.GetCommentListStart,- status: 0,- });-- // 2. 循环AI回复评论+ // 1. 循环AI回复评论for (const works of worksList) {sleep(5);const oldRecord = await this.getInteractionRecord(@@ -145,6 +131,7 @@ export class InteractionService {status: -1,error: '未获得AI产出内容',});+ cacheData.delete();return false;}@@ -248,7 +235,7 @@ export class InteractionService {}// 清除缓存- GlobleCache.delCache(getCacheKey());+ cacheData.delete();}/**@@ -268,7 +255,7 @@ export class InteractionService {message?: string;}> {// 查看缓存,有的就不执行- if (GlobleCache.getCache(getCacheKey())) {+ if (AutoInteractionCache.getInfo()) {sysNotice('请勿重复执行', `有正在执行的任务,任务ID:${autoRun.id}`);return {diff --git a/src/icp/replyother.ts b/src/icp/replyother.tsindex eb3ac18e..2bd3c2a2 100644--- a/src/icp/replyother.ts+++ b/src/icp/replyother.ts@@ -238,3 +238,11 @@ export async function ipcCreateAutoRunOfInteraction();return res;}++// 获取一键互动自动进程信息+export async function ipcGetAutoRunOfInteractionInfo() {+ const res: any = await window.ipcRenderer.invoke(+ 'ICP_GET_AUTO_INTERACTION_INFO',+ );+ return res;+}