diff --git a/src/redis/redis.service.ts b/src/redis/redis.service.ts index dd6ae50..b8e8af1 100644 --- a/src/redis/redis.service.ts +++ b/src/redis/redis.service.ts @@ -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 { + if (!this.redis) { + return null; + } + + return await this.redis.get(key); + } + + async del(key: string): Promise { + if (!this.redis) { + return null; + } + + return await this.redis.del(key); + } + + getClient(): Redis | null { + return this.redis; + } } diff --git a/src/vm/plugins.constants.ts b/src/vm/plugins.constants.ts index 3b9854d..ccbb372 100644 --- a/src/vm/plugins.constants.ts +++ b/src/vm/plugins.constants.ts @@ -1,5 +1,7 @@ import { DatabasePlugin } from "./plugins/database.plugin"; +import { RedisPlugin } from "./plugins/redis.plugin"; export const PluginClass = { DATABASE: DatabasePlugin, + REDIS: RedisPlugin, }; diff --git a/src/vm/plugins/redis.plugin.ts b/src/vm/plugins/redis.plugin.ts new file mode 100644 index 0000000..685f2f7 --- /dev/null +++ b/src/vm/plugins/redis.plugin.ts @@ -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 { + return this.redisClient; + } + + onFinish(): void { + this.redisClient.quit(); + } +}