Skip to content

Commit

Permalink
feat: redis client base class
Browse files Browse the repository at this point in the history
  • Loading branch information
shakkernerd committed Dec 20, 2024
1 parent 2756c09 commit 2bfed97
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions packages/adapter-redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Redis from "ioredis";
import { IDatabaseCacheAdapter, UUID } from "@ai16z/eliza";

export class RedisClient implements IDatabaseCacheAdapter {
private client: Redis;

constructor() {
this.client = new Redis();

this.client.on("connect", () => {
console.log("Connected to Redis");
});

this.client.on("error", (err) => {
console.error("Redis error:", err);
});
}

async getCache(params: {
agentId: UUID;
key: string;
}): Promise<string | undefined> {
try {
const redisKey = this.buildKey(params.agentId, params.key);
const value = await this.client.get(redisKey);
return value || undefined;
} catch (err) {
console.error("Error getting cache:", err);
return undefined;
}
}

async setCache(params: {
agentId: UUID;
key: string;
value: string;
}): Promise<boolean> {
try {
const redisKey = this.buildKey(params.agentId, params.key);
await this.client.set(redisKey, params.value);
return true;
} catch (err) {
console.error("Error setting cache:", err);
return false;
}
}

async deleteCache(params: {
agentId: UUID;
key: string;
}): Promise<boolean> {
try {
const redisKey = this.buildKey(params.agentId, params.key);
const result = await this.client.del(redisKey);
return result > 0;
} catch (err) {
console.error("Error deleting cache:", err);
return false;
}
}

async disconnect(): Promise<void> {
try {
await this.client.quit();
console.log("Disconnected from Redis");
} catch (err) {
console.error("Error disconnecting from Redis:", err);
}
}

private buildKey(agentId: UUID, key: string): string {
return `${agentId}:${key}`; // Constructs a unique key based on agentId and key
}
}

export default RedisClient;

0 comments on commit 2bfed97

Please sign in to comment.