feat(redis api client): improve

This commit is contained in:
Jozef Steinhübl 2024-12-14 21:51:14 +01:00
parent 6678ded2f9
commit 3cd6065ab8
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 137 additions and 70 deletions

BIN
bun.lockb Executable file

Binary file not shown.

Binary file not shown.

View file

@ -10,5 +10,8 @@
"devDependencies": { "devDependencies": {
"esbuild": "^0.15.11", "esbuild": "^0.15.11",
"typescript": "^4.8.4" "typescript": "^4.8.4"
},
"dependencies": {
"@upstash/redis": "^1.34.3"
} }
} }

View file

@ -1,4 +1,66 @@
export class RedisAPIClient { interface RedisClient {
connect(): Promise<void> | void;
get(key: string): Promise<string | null>;
set(key: string, value: string): Promise<string>;
setex(key: string, value: string, seconds: number): Promise<string>;
del(key: string): Promise<number>;
}
export class Upstash implements RedisClient {
private url: string;
private token: string;
private redis: import("@upstash/redis").Redis | undefined;
constructor(url: string, token: string) {
this.url = url;
this.token = token;
this.redis = undefined;
}
public async connect() {
if (this.redis) {
return;
}
this.redis = new (await import("@upstash/redis")).Redis({
url: this.url,
token: this.token,
});
}
public async get(key: string): Promise<string | null> {
await this.connect();
return (await this.redis?.get(key)) ?? null;
}
public async set(key: string, value: string): Promise<string> {
await this.connect();
return (await this.redis?.set(key, value)) ?? "OK";
}
public async setex(
key: string,
value: string,
seconds: number
): Promise<string> {
await this.connect();
return (await this.redis?.setex(key, seconds, value)) ?? "OK";
}
public async del(key: string): Promise<number> {
await this.connect();
return (await this.redis?.del(key)) ?? 0;
}
}
export class Raw implements RedisClient {
private apiKey: string; private apiKey: string;
private host: string; private host: string;
@ -7,6 +69,8 @@ export class RedisAPIClient {
this.host = host; this.host = host;
} }
public connect() {}
public async get(key: string): Promise<string | null> { public async get(key: string): Promise<string | null> {
const url = `${this.host}/get?key=${key}`; const url = `${this.host}/get?key=${key}`;
const response = await fetch(url, { const response = await fetch(url, {
@ -39,7 +103,7 @@ export class RedisAPIClient {
public async setex( public async setex(
key: string, key: string,
value: string, value: string,
seconds: number, seconds: number
): Promise<string> { ): Promise<string> {
const url = `${this.host}/setex`; const url = `${this.host}/setex`;
const response = await fetch(url, { const response = await fetch(url, {
@ -58,7 +122,7 @@ export class RedisAPIClient {
return response.text(); return response.text();
} }
public async del(key: string): Promise<string> { public async del(key: string): Promise<number> {
const url = `${this.host}/del?key=${key}`; const url = `${this.host}/del?key=${key}`;
const response = await fetch(url, { const response = await fetch(url, {
method: "DELETE", method: "DELETE",
@ -67,6 +131,6 @@ export class RedisAPIClient {
}, },
}); });
return response.text(); return parseInt(await response.text());
} }
} }