Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/drivers/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ const driver: DriverFactory<RedisOptions, Redis | Cluster> = (opts) => {
await getRedisClient().set(p(key), value);
}
},
async setItems(items, commonOptions) {
const defaultTtl = commonOptions?.ttl ?? opts.ttl;
// `MSET` cannot set a per-key TTL, so fall back to a pipeline of
// `SET ... EX` (mirroring `setItem`) whenever a TTL applies; otherwise
// use a single `MSET` for efficiency.
const hasTtl = defaultTtl || items.some((item) => item.options?.ttl);
if (hasTtl) {
const pipeline = getRedisClient().pipeline();
for (const item of items) {
const ttl = item.options?.ttl ?? defaultTtl;
if (ttl) {
pipeline.set(p(item.key), item.value, "EX", ttl);
} else {
pipeline.set(p(item.key), item.value);
}
}
await pipeline.exec();
} else {
const args: string[] = [];
for (const item of items) {
args.push(p(item.key), item.value);
}
await getRedisClient().mset(...args);
}
},
async setItemRaw(key, value, tOptions) {
const _value = normalizeValue(value);
const ttl = tOptions?.ttl ?? opts.ttl;
Expand Down
18 changes: 18 additions & 0 deletions test/drivers/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ describe("drivers: redis", () => {

await client.disconnect();
});

it("setItems with ttl sets each key with an expiry", async () => {
await ctx.storage.setItems(
[
{ key: "s6:a", value: "a" },
{ key: "s6:b", value: "b" },
],
{ ttl: 1000 },
);

const client = new (ioredisMock as any).default("ioredis://localhost:6379/0");

expect(await client.get("test:s6:a")).toBe("a");
expect(await client.get("test:s6:b")).toBe("b");
expect(await client.ttl("test:s6:a")).toBeGreaterThan(0);

await client.disconnect();
});
Comment on lines +43 to +59

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Verify TTL for all keys, not just the first.

The test asserts TTL for "test:s6:a" (line 56) but not for "test:s6:b", even though the test name promises "sets each key with an expiry." If the implementation inadvertently applied TTL only to the first key in the batch, this test would not detect the bug.

🧪 Proposed fix to verify both keys
 expect(await client.get("test:s6:a")).toBe("a");
 expect(await client.get("test:s6:b")).toBe("b");
 expect(await client.ttl("test:s6:a")).toBeGreaterThan(0);
+expect(await client.ttl("test:s6:b")).toBeGreaterThan(0);

 await client.disconnect();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("setItems with ttl sets each key with an expiry", async () => {
await ctx.storage.setItems(
[
{ key: "s6:a", value: "a" },
{ key: "s6:b", value: "b" },
],
{ ttl: 1000 },
);
const client = new (ioredisMock as any).default("ioredis://localhost:6379/0");
expect(await client.get("test:s6:a")).toBe("a");
expect(await client.get("test:s6:b")).toBe("b");
expect(await client.ttl("test:s6:a")).toBeGreaterThan(0);
await client.disconnect();
});
it("setItems with ttl sets each key with an expiry", async () => {
await ctx.storage.setItems(
[
{ key: "s6:a", value: "a" },
{ key: "s6:b", value: "b" },
],
{ ttl: 1000 },
);
const client = new (ioredisMock as any).default("ioredis://localhost:6379/0");
expect(await client.get("test:s6:a")).toBe("a");
expect(await client.get("test:s6:b")).toBe("b");
expect(await client.ttl("test:s6:a")).toBeGreaterThan(0);
expect(await client.ttl("test:s6:b")).toBeGreaterThan(0);
await client.disconnect();
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/drivers/redis.test.ts` around lines 43 - 59, Update the test "setItems
with ttl sets each key with an expiry" to assert TTL for every key in the batch:
after calling ctx.storage.setItems(...) and creating the ioredisMock client
(ioredisMock.default(...)), verify both values with client.get("test:s6:a") and
client.get("test:s6:b") and assert client.ttl("test:s6:a") and
client.ttl("test:s6:b") are > 0; keep using ctx.storage.setItems and
client.disconnect as currently used.

},
});
});