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