返回 AiToEarn
cacheData.ts
1 import { GlobleCache } from '../../global/cache';
2
3 // 0 进行中 1 完成 2 错误
4 export enum AutoInteractionCacheStatus {
5 DOING = 0,
6 DONE = 1,
7 REEOR = 2,
8 }
9
10 export type AutoReplyCacheData = {
11 status: AutoInteractionCacheStatus;
12 message: string;
13 createTime?: number;
14 updateTime?: number;
15 title: string;
16 dataId?: string;
17 };
18
19 export class AutoInteractionCache {
20 static cacheKey = 'OneKeyInteractionWorksCacheKey';
21 constructor(data: { title: string }) {
22 const cacheData = {
23 status: AutoInteractionCacheStatus.DOING,
24 message: '进行中',
25 updateTime: new Date().getTime(),
26 createTime:
27 (
28 GlobleCache.getCache(
29 AutoInteractionCache.cacheKey,
30 ) as AutoReplyCacheData
31 )?.createTime || new Date().getTime(),
32 ...data,
33 };
34
35 GlobleCache.setCache(AutoInteractionCache.cacheKey, cacheData, 60 * 30); // 设置缓存
36 }
37
38 // 获取信息
39 static getInfo() {
40 return GlobleCache.getCache(
41 AutoInteractionCache.cacheKey,
42 ) as AutoReplyCacheData | null;
43 }
44
45 // 延长ttl
46 extendTTL() {
47 GlobleCache.updateCacheTTL(AutoInteractionCache.cacheKey, 60 * 30); // 重设缓存时间
48 }
49
50 // 更新状态
51 updateStatus(status: AutoInteractionCacheStatus, message?: string) {
52 const cacheData = GlobleCache.getCache(AutoInteractionCache.cacheKey);
53 if (cacheData) {
54 GlobleCache.setCache(AutoInteractionCache.cacheKey, {
55 ...cacheData,
56 status,
57 message,
58 updateTime: new Date().getTime(),
59 });
60 }
61 }
62
63 // 删除
64 delete() {
65 GlobleCache.delCache(AutoInteractionCache.cacheKey);
66 }
67 }
68
68 lines TYPESCRIPT