Skip to content

Commit

Permalink
fix: 🚑 resolve get key error (#998)
Browse files Browse the repository at this point in the history
This PR resolves the get/set key for redis
  • Loading branch information
james-a-morris authored Oct 13, 2023
1 parent 948013f commit d078397
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/utils/RedisUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ export type _RedisClient = ReturnType<typeof createClient>;
export class RedisClient {
constructor(private readonly client: _RedisClient, private readonly namespace?: string) {}

private getNamespacedKey(key: string): string {
return isDefined(this.namespace) ? `${this.namespace}:${key}` : key;
}

async get(key: string): Promise<string | undefined> {
return this.client.get(`${this.namespace}:${key}}`);
return this.client.get(this.getNamespacedKey(key));
}

async set(key: string, val: string, expirySeconds = constants.DEFAULT_CACHING_TTL): Promise<void> {
const modifiedKey = isDefined(this.namespace) ? `${this.namespace}:${key}` : key;
if (expirySeconds > 0) {
// EX: Expire key after expirySeconds.
await this.client.set(modifiedKey, val, { EX: expirySeconds });
await this.client.set(this.getNamespacedKey(key), val, { EX: expirySeconds });
} else {
await this.client.set(modifiedKey, val);
await this.client.set(this.getNamespacedKey(key), val);
}
}

Expand Down

0 comments on commit d078397

Please sign in to comment.