Skip to content

feat(redis): support setItems (#705)#782

Open
Ruby-Leung wants to merge 1 commit into
unjs:mainfrom
Ruby-Leung:feat/redis-setitems
Open

feat(redis): support setItems (#705)#782
Ruby-Leung wants to merge 1 commit into
unjs:mainfrom
Ruby-Leung:feat/redis-setitems

Conversation

@Ruby-Leung

@Ruby-Leung Ruby-Leung commented Jun 4, 2026

Copy link
Copy Markdown

Closes #705.

The redis driver already implements getItems (via MGET), so this adds the symmetric setItems:

  • No TTL → a single MSET (efficient bulk write).
  • TTL set (per-item options.ttl, commonOptions.ttl, or the driver's opts.ttl) → a pipeline of SET ... EX, mirroring setItem, since MSET cannot set a per-key TTL.

Covered by the shared driver setItems test (the MSET path) plus a redis-specific test for the TTL/pipeline path.

Summary by CodeRabbit

  • New Features
    • Redis driver now supports batch operations for setting multiple items with optional TTL (time-to-live) configuration.

The redis driver already implements getItems via MSET's counterpart MGET; this adds the symmetric setItems. It uses a single MSET when no TTL applies, and falls back to a pipeline of SET ... EX (mirroring setItem) when a TTL is set per-item or via commonOptions/opts, since MSET cannot set a per-key TTL.
@Ruby-Leung Ruby-Leung requested a review from pi0 as a code owner June 4, 2026 11:52
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds setItems support to the Redis driver. A new async method computes default TTL from options and uses an optimized strategy: when TTLs apply, it batches SET commands with expiry via an ioredis pipeline; when no TTLs exist, it uses a single MSET call for efficiency. A test validates that stored keys receive correct values and non-zero TTL.

Changes

Redis setItems Feature

Layer / File(s) Summary
setItems implementation and test
src/drivers/redis.ts, test/drivers/redis.test.ts
Redis driver adds async setItems(items, commonOptions) that computes TTL from commonOptions?.ttl ?? opts.ttl, detects if any item has a TTL, and uses an ioredis pipeline for SET ... EX <ttl> when TTLs apply or a single MSET call when none apply. Test verifies multiple keys are stored with correct values and positive TTL expiry.

🎯 3 (Moderate) | ⏱️ ~20 minutes


🐰 A redis driver hops with pride,
setItems batch sends values far and wide,
TTL or MSET, each path well-tried,
Multiple keys stored, verified! 🚀

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding setItems support to the Redis driver, which is the primary objective of this PR.
Linked Issues check ✅ Passed The implementation fulfills issue #705 by adding setItems to the Redis driver using ioredis.mset for no-TTL cases and per-key SET commands with TTL when needed.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing setItems for the Redis driver as specified in issue #705; no unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with 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.

Inline comments:
In `@test/drivers/redis.test.ts`:
- Around line 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.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a20272a8-97cc-43b6-a743-1a25f1230917

📥 Commits

Reviewing files that changed from the base of the PR and between 2727956 and 7923bcf.

📒 Files selected for processing (2)
  • src/drivers/redis.ts
  • test/drivers/redis.test.ts

Comment on lines +43 to +59
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();
});

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add setItems support to redis driver

1 participant