-
-
Notifications
You must be signed in to change notification settings - Fork 340
feat: add REDIS_KEY_PREFIX env var for Redis ACL namespace isolation #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
207ae9e
4883747
5d3c146
9439041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,31 @@ import { env } from "~/env"; | |
|
|
||
| export let connection: IORedis | null = null; | ||
|
|
||
| /** | ||
| * Key prefix derived from REDIS_KEY_PREFIX env var. | ||
| * When set (e.g. "usesend"), all cache keys become "usesend:team:1", etc. | ||
| * When empty, keys are unprefixed (backwards compatible). | ||
| */ | ||
| export const REDIS_PREFIX = env.REDIS_KEY_PREFIX | ||
| ? `${env.REDIS_KEY_PREFIX}:` | ||
| : ""; | ||
|
Comment on lines
+11
to
+13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize If Proposed fix+const NORMALIZED_REDIS_KEY_PREFIX = env.REDIS_KEY_PREFIX.replace(/:+$/, "");
+
-export const REDIS_PREFIX = env.REDIS_KEY_PREFIX
- ? `${env.REDIS_KEY_PREFIX}:`
+export const REDIS_PREFIX = NORMALIZED_REDIS_KEY_PREFIX
+ ? `${NORMALIZED_REDIS_KEY_PREFIX}:`
: "";
@@
-export const BULL_PREFIX = env.REDIS_KEY_PREFIX || "bull";
+export const BULL_PREFIX = NORMALIZED_REDIS_KEY_PREFIX || "bull";Also applies to: 19-19 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged — this is a reasonable defensive measure, but out of scope for this PR. The Back to you to resolve.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🧠 Learnings used
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for a follow-up issue, this is out of scope.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🧠 Learnings used |
||
|
|
||
| /** | ||
| * BullMQ prefix (no trailing colon — BullMQ adds its own separator). | ||
| * When REDIS_KEY_PREFIX is empty, falls back to BullMQ's default "bull". | ||
| */ | ||
| export const BULL_PREFIX = env.REDIS_KEY_PREFIX || "bull"; | ||
|
|
||
| /** Prefix a cache key with REDIS_KEY_PREFIX. */ | ||
| export function redisKey(key: string): string { | ||
| return `${REDIS_PREFIX}${key}`; | ||
| } | ||
|
|
||
| export const getRedis = () => { | ||
| if (!connection || connection.status === "end") { | ||
| connection = new IORedis(`${env.REDIS_URL}?family=0`, { | ||
| maxRetriesPerRequest: null, | ||
| enableReadyCheck: false, | ||
| }); | ||
| } | ||
| return connection; | ||
|
|
@@ -24,9 +45,10 @@ export async function withCache<T>( | |
| const { ttlSeconds = 120, disable = false } = options ?? {}; | ||
|
|
||
| const redis = getRedis(); | ||
| const prefixedKey = redisKey(key); | ||
|
|
||
| if (!disable) { | ||
| const cached = await redis.get(key); | ||
| const cached = await redis.get(prefixedKey); | ||
| if (cached) { | ||
| try { | ||
| return JSON.parse(cached) as T; | ||
|
|
@@ -40,7 +62,7 @@ export async function withCache<T>( | |
|
|
||
| if (!disable) { | ||
| try { | ||
| await redis.setex(key, ttlSeconds, JSON.stringify(value)); | ||
| await redis.setex(prefixedKey, ttlSeconds, JSON.stringify(value)); | ||
| } catch { | ||
| // ignore cache set errors | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.