Skip to content

Commit

Permalink
feat(redis-driver): add more func
Browse files Browse the repository at this point in the history
  • Loading branch information
Vann-Dev committed Jan 2, 2024
1 parent 0f4af5a commit 36f48d5
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions drivers/redis-driver/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,12 +9,34 @@ export class RedisPlayerDriver implements PlayerCacheDriver {
}

public async get(clientId: string, guildId: string): Promise<PlayerData | null> {
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<PlayerData> {
await this.redis.set(`${clientId}:${guildId}`, JSON.stringify(data));
return data;
}

public async delete(clientId: string, guildId: string): Promise<void> {
await this.redis.del(`${clientId}:${guildId}`);
}

// TODO: check if this working?
public async values(clientId: string, count = 1000): Promise<PlayerData[]> {
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<number> {
const data = await redisScan(this.redis, clientId, 1000);
return data.length;
}
}

0 comments on commit 36f48d5

Please sign in to comment.