Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf: add prompt key index (#2793)

Hassieb Pakzad24ec7884a17e
diff --git a/packages/shared/src/server/services/PromptService.ts b/packages/shared/src/server/services/PromptService.ts
index e7192478ad..c57c3ea5fd 100644
--- a/packages/shared/src/server/services/PromptService.ts
+++ b/packages/shared/src/server/services/PromptService.ts
@@ -111,9 +111,11 @@ export class PromptService {
private async cachePrompt(params: PromptParams & { prompt: Prompt }) {
try {
+ const keyIndexKey = this.getKeyIndexKey(params);
const key = this.getCacheKey(params);
const value = JSON.stringify(params.prompt);
+ await this.redis?.sadd(keyIndexKey, key);
await this.redis?.set(key, value, "EX", this.ttlSeconds);
} catch (e) {
this.logError("Error caching prompt", e);
@@ -183,7 +185,12 @@ export class PromptService {
try {
const startTime = Date.now();
this.logInfo("Invalidating cache for prefix", cacheKeyPrefix);
- await this.deleteKeysByPrefix(cacheKeyPrefix);
+
+ const keyIndexKey = this.getKeyIndexKey(params);
+ const keys = await this.redis?.smembers(keyIndexKey);
+
+ // Delete all keys for the prefix and the key index
+ await this.redis?.del([...(keys ?? []), keyIndexKey]);
this.logInfo(
`Cache invalidated for prefix ${cacheKeyPrefix} in ${Date.now() - startTime}ms`
@@ -207,23 +214,10 @@ export class PromptService {
return `prompt:${params.projectId}:${params.promptName}`;
}
- private async deleteKeysByPrefix(prefix: string): Promise<void> {
- const script = `
- local cursor = "0"
- repeat
- local result = redis.call("SCAN", cursor, "MATCH", ARGV[1] .. "*", "COUNT", 100)
-
- cursor = result[1]
- local keys = result[2]
-
- if #keys > 0 then
- redis.call("DEL", unpack(keys))
- end
-
- until cursor == "0"
- `;
-
- await this.redis?.eval(script, 0, prefix);
+ private getKeyIndexKey(
+ params: Pick<PromptParams, "projectId" | "promptName">
+ ): string {
+ return `prompt_key_index:${params.projectId}:${params.promptName}`;
}
private logError(message: string, ...args: any[]) {
diff --git a/web/src/__tests__/promptCache.servertest.ts b/web/src/__tests__/promptCache.servertest.ts
index 28ee3f7b0c..db9e1255e4 100644
--- a/web/src/__tests__/promptCache.servertest.ts
+++ b/web/src/__tests__/promptCache.servertest.ts
@@ -47,6 +47,8 @@ describe("PromptService", () => {
del: jest.fn(),
exists: jest.fn(),
eval: jest.fn(),
+ sadd: jest.fn(),
+ smembers: jest.fn(),
} as unknown as jest.Mocked<Redis>;
mockMetricIncrementer = jest.fn();
@@ -93,6 +95,18 @@ describe("PromptService", () => {
"prompt_cache_miss",
1,
);
+
+ expect(mockRedis.set).toHaveBeenCalledWith(
+ "prompt:project1:testPrompt:1",
+ JSON.stringify(mockPrompt),
+ "EX",
+ 3600,
+ );
+
+ expect(mockRedis.sadd).toHaveBeenCalledWith(
+ "prompt_key_index:project1:testPrompt",
+ "prompt:project1:testPrompt:1",
+ );
});
it("should not use cache if locked", async () => {
@@ -147,10 +161,8 @@ describe("PromptService", () => {
promptName: "testPrompt",
});
- expect(mockRedis.eval).toHaveBeenCalledWith(
- expect.any(String),
- 0,
- "prompt:project1:testPrompt",
+ expect(mockRedis.smembers).toHaveBeenCalledWith(
+ "prompt_key_index:project1:testPrompt",
);
});
});
@@ -296,7 +308,7 @@ describe("PromptService", () => {
describe("invalidateCache with Redis errors", () => {
it("should throw an error if Redis.eval fails", async () => {
- mockRedis.eval.mockRejectedValue(new Error("Redis error"));
+ mockRedis.smembers.mockRejectedValue(new Error("Redis error"));
await expect(
promptService.invalidateCache({