feat: implement Redis client and plugin with basic operations
This commit is contained in:
@ -9,4 +9,40 @@ export class RedisClient {
|
||||
constructor(private readonly redisService: RedisService) {
|
||||
this.redis = this.redisService.getOrThrow();
|
||||
}
|
||||
|
||||
async set(
|
||||
key: string,
|
||||
value: string,
|
||||
expireInSeconds?: number
|
||||
): Promise<"OK" | null> {
|
||||
if (!this.redis) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (expireInSeconds) {
|
||||
return await this.redis.set(key, value, "EX", expireInSeconds);
|
||||
}
|
||||
|
||||
return await this.redis.set(key, value);
|
||||
}
|
||||
|
||||
async get(key: string): Promise<string | null> {
|
||||
if (!this.redis) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await this.redis.get(key);
|
||||
}
|
||||
|
||||
async del(key: string): Promise<number | null> {
|
||||
if (!this.redis) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await this.redis.del(key);
|
||||
}
|
||||
|
||||
getClient(): Redis | null {
|
||||
return this.redis;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import { DatabasePlugin } from "./plugins/database.plugin";
|
||||
import { RedisPlugin } from "./plugins/redis.plugin";
|
||||
|
||||
export const PluginClass = {
|
||||
DATABASE: DatabasePlugin,
|
||||
REDIS: RedisPlugin,
|
||||
};
|
||||
|
||||
28
src/vm/plugins/redis.plugin.ts
Normal file
28
src/vm/plugins/redis.plugin.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import Redis from "ioredis";
|
||||
import { Plugin } from "../plugin.class";
|
||||
|
||||
export class RedisPlugin extends Plugin {
|
||||
constructor(name: string, private redisClient: Redis) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
static init(
|
||||
name: string,
|
||||
config: { host: string; port: number }
|
||||
): RedisPlugin {
|
||||
const redisClient = new Redis({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
});
|
||||
|
||||
return new RedisPlugin(name, redisClient);
|
||||
}
|
||||
|
||||
async run(): Promise<Redis> {
|
||||
return this.redisClient;
|
||||
}
|
||||
|
||||
onFinish(): void {
|
||||
this.redisClient.quit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user