diff --git a/drivers/redis-driver/src/index.ts b/drivers/redis-driver/src/index.ts index adb3662..a1dc59f 100644 --- a/drivers/redis-driver/src/index.ts +++ b/drivers/redis-driver/src/index.ts @@ -1,5 +1,6 @@ import { PlayerCacheDriver, PlayerData } from "@kirishima/core-driver"; import { Redis, RedisOptions } from "ioredis"; +import { redisScan } from "@nezuchan/utilities"; export class RedisPlayerDriver implements PlayerCacheDriver { public redis: Redis; @@ -8,12 +9,34 @@ export class RedisPlayerDriver implements PlayerCacheDriver { } public async get(clientId: string, guildId: string): Promise { - const data = await this.redis.get(`${clientId}:${guildId}`); + const data = (await this.redis.get(`${clientId}:${guildId}`))!; - if (data) { - - }; + if (!data) return null; - return null; + const parsedData: PlayerData = JSON.parse(data); + return parsedData; + } + + public async set(clientId: string, guildId: string, data: PlayerData): Promise { + await this.redis.set(`${clientId}:${guildId}`, JSON.stringify(data)); + return data; + } + + public async delete(clientId: string, guildId: string): Promise { + await this.redis.del(`${clientId}:${guildId}`); + } + + // TODO: check if this working? + public async values(clientId: string, count = 1000): Promise { + const data = await redisScan(this.redis, clientId, count); + const result: PlayerData[] = data.map(val => JSON.parse(val)); + + return result; + } + + // TODO: unlimit redis scan + public async size(clientId: string): Promise { + const data = await redisScan(this.redis, clientId, 1000); + return data.length; } }