-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcache.ts
126 lines (103 loc) · 3.27 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import path from "path";
import fs from "fs/promises";
import type {
CacheOptions,
ICacheManager,
IDatabaseCacheAdapter,
UUID,
} from "./types";
export interface ICacheAdapter {
get(key: string): Promise<string | undefined>;
set(key: string, value: string): Promise<void>;
delete(key: string): Promise<void>;
}
export class MemoryCacheAdapter implements ICacheAdapter {
data: Map<string, string>;
constructor(initalData?: Map<string, string>) {
this.data = initalData ?? new Map<string, string>();
}
async get(key: string): Promise<string | undefined> {
return this.data.get(key);
}
async set(key: string, value: string): Promise<void> {
this.data.set(key, value);
}
async delete(key: string): Promise<void> {
this.data.delete(key);
}
}
export class FsCacheAdapter implements ICacheAdapter {
constructor(private dataDir: string) {}
async get(key: string): Promise<string | undefined> {
try {
return await fs.readFile(path.join(this.dataDir, key), "utf8");
} catch {
// console.error(error);
return undefined;
}
}
async set(key: string, value: string): Promise<void> {
try {
const filePath = path.join(this.dataDir, key);
// Ensure the directory exists
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, value, "utf8");
} catch (error) {
console.error(error);
}
}
async delete(key: string): Promise<void> {
try {
const filePath = path.join(this.dataDir, key);
await fs.unlink(filePath);
} catch {
// console.error(error);
}
}
}
export class DbCacheAdapter implements ICacheAdapter {
constructor(
private db: IDatabaseCacheAdapter,
private agentId: UUID
) {}
async get(key: string): Promise<string | undefined> {
return this.db.getCache({ agentId: this.agentId, key });
}
async set(key: string, value: string): Promise<void> {
await this.db.setCache({ agentId: this.agentId, key, value });
}
async delete(key: string): Promise<void> {
await this.db.deleteCache({ agentId: this.agentId, key });
}
}
export class CacheManager<CacheAdapter extends ICacheAdapter = ICacheAdapter>
implements ICacheManager
{
adapter: CacheAdapter;
constructor(adapter: CacheAdapter) {
this.adapter = adapter;
}
async get<T = unknown>(key: string): Promise<T | undefined> {
const data = await this.adapter.get(key);
if (data) {
const { value, expires } = JSON.parse(data) as {
value: T;
expires: number;
};
if (!expires || expires > Date.now()) {
return value;
}
this.adapter.delete(key).catch(() => {});
}
return undefined;
}
async set<T>(key: string, value: T, opts?: CacheOptions): Promise<void> {
return this.adapter.set(
key,
JSON.stringify({ value, expires: opts?.expires ?? 0 })
);
}
async delete(key: string): Promise<void> {
return this.adapter.delete(key);
}
}