forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
205 lines (176 loc) · 4.95 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import Redis from 'ioredis';
import { Config } from './config';
import { newLogger } from './log';
import { Logger } from 'winston';
import { parseJSON } from './index';
export let DefaultRedis: Redis;
export function initCache() {
if (!Config.config.global?.redis?.host) {
setTimeout(() => initCache(), 5000);
return;
}
DefaultRedis = new Redis(Config.config.global.redis);
DefaultRedis.on('ready', () => {
console.info(
`redis[${Config.config.global.redis.host}:${Config.config.global.redis.port}] ready`,
);
});
DefaultRedis.on('error', (e) => {
console.debug(
`redis[${Config.config.global.redis.host}:${Config.config.global.redis.port}] failed,${e.message}`,
);
});
}
export class StringPool {
private redis: Redis;
private readonly key: string;
private logger!: Logger;
constructor(redis: Redis, key: string) {
this.redis = redis;
this.key = key;
this.logger = newLogger(`${this.key}`);
}
async add(value: string): Promise<number> {
this.logger.debug(`add ${value}`);
return this.redis.sadd(this.key, value);
}
async remove(value: string): Promise<void> {
this.logger.debug(`remove ${value}`);
await this.redis.srem(this.key, value);
}
async random(): Promise<string | null> {
this.logger.debug(`random`);
return this.redis.srandmember(this.key);
}
async size(): Promise<number> {
this.logger.debug(`size`);
return this.redis.scard(this.key);
}
async clear(): Promise<void> {
this.logger.debug(`clear`);
await this.redis.del(this.key);
}
// pop
async pop(): Promise<string | null> {
this.logger.debug(`pop`);
return this.redis.spop(this.key);
}
}
export class MemoryStringPool {
private pool: Set<string> = new Set();
async add(value: string): Promise<number> {
this.pool.add(value);
return this.pool.size;
}
async remove(value: string): Promise<void> {
this.pool.delete(value);
}
async random(): Promise<string | null> {
const arr = Array.from(this.pool);
if (arr.length === 0) {
return null;
}
return arr[Math.floor(Math.random() * arr.length)];
}
async size(): Promise<number> {
return this.pool.size;
}
async clear(): Promise<void> {
this.pool.clear();
}
async pop(): Promise<string | null> {
const arr = Array.from(this.pool);
if (arr.length === 0) {
return null;
}
const v = arr[Math.floor(Math.random() * arr.length)];
this.pool.delete(v);
return v;
}
}
// string类型的key,传入初始化方法
// 如果key不存在,会调用init方法初始化
// key需要有过期时间
// 防止缓存穿透和缓存雪崩
export class CommCache<T> {
private redis: Redis;
private readonly _key: string;
private readonly init?: () => Promise<T | null>;
private readonly expire: number;
private logger!: Logger;
constructor(
redis: Redis,
key: string,
expire: number,
init?: () => Promise<T | null>,
) {
this.redis = redis;
this._key = key;
this.init = init;
this.expire = expire;
this.logger = newLogger(`${this._key}`);
}
key(subkey: string) {
return this._key + ':' + subkey;
}
async get(subkey: string, init?: () => Promise<T | null>): Promise<T | null> {
if (!init && !this.init) {
throw new Error('init is null');
}
const initFunc = init || this.init;
const v = await this.redis.get(this.key(subkey));
if (v) {
this.logger.debug(`${subkey} cache got`);
return parseJSON<T | null>(v, null);
}
const nv = await initFunc!();
const sv = JSON.stringify(nv);
await this.redis.set(this.key(subkey), sv, 'EX', this.expire);
this.logger.debug(`${subkey} cache miss`);
return nv;
}
async set(subkey: string, value: string): Promise<void> {
this.logger.debug(`set ${value}`);
await this.redis.set(this.key(subkey), value, 'EX', this.expire);
}
async clear(subkey: string): Promise<void> {
this.logger.debug(`clear`);
await this.redis.del(this.key(subkey));
}
}
export class StringCache<T> {
private redis: Redis;
private readonly _key: string;
private readonly expire: number;
private logger!: Logger;
constructor(redis: Redis, key: string, expire: number) {
this.redis = redis;
this._key = key;
this.expire = expire;
this.logger = newLogger(`${this._key}`);
}
key(subkey: string) {
return this._key + ':' + subkey;
}
async get(subkey: string): Promise<T | null> {
const v = await this.redis.get(this.key(subkey));
if (!v) {
return null;
}
this.logger.debug(`${subkey} cache got`);
return parseJSON<T | null>(v, v as T);
}
async set(subkey: string, value: T): Promise<void> {
this.logger.debug(`set ${subkey}:${value}`);
await this.redis.set(
this.key(subkey),
JSON.stringify(value),
'EX',
this.expire,
);
}
async clear(subkey: string): Promise<void> {
this.logger.debug(`clear`);
await this.redis.del(this.key(subkey));
}
}