Skip to content
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

Support dynamic generation of cacheKeyPrefix #29

Open
Tam2 opened this issue Jul 23, 2024 · 1 comment
Open

Support dynamic generation of cacheKeyPrefix #29

Tam2 opened this issue Jul 23, 2024 · 1 comment
Labels
core good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Tam2
Copy link

Tam2 commented Jul 23, 2024

It would be good to dynamically generate the cacheKeyPrefix based on the incoming request

The use case would be to set the cacheKeyPrefix to the tenantId for the incoming request so each tenant has its own namespace for the keys

For us the tenantId would either be in the header or as part of the JWT based on how the user is authenticated

@mahendraHegde mahendraHegde added help wanted Extra attention is needed good first issue Good for newcomers core labels Jul 24, 2024
@mahendraHegde mahendraHegde changed the title Support multi tenant Support dynamic generation of cacheKeyPrefix Jul 24, 2024
@mahendraHegde
Copy link
Owner

@Tam2 have you considered passing options to IdempotencyParams?
you can also refer this test case.

it("should allow overriding cacheKeyPrefix", async () => {
const req: IdempotencyParams = {
headers: { [HttpHeaderKeysEnum.IDEMPOTENCY_KEY]: "1" },
path: "/pay",
body: { a: "a" },
method: "POST",
options: {
cacheKeyPrefix: "tenant-1",
},
};
const res = { body: { success: "true" } };
const idempotencyRes = await idempotency.onRequest(req);
expect(idempotencyRes).toBeUndefined();
await idempotency.onResponse(req, res);
const cachedResponse = await idempotency.onRequest(req);
expect(cachedResponse).toEqual(res);
expect(
await storage.get(`${req.options?.cacheKeyPrefix}:POST:/pay:1`),
).toEqual(
'{"status":"COMPLETE","fingerPrint":"f45aa7a9803525391d546d331b22b3ed4583a11a04797feaeb1027b158c65d10","response":{"body":{"success":"true"}}}',
);
// different tenant with same idempotency key
const cachedResponse2 = await idempotency.onRequest({
...req,
options: {
cacheKeyPrefix: "tenant-2",
},
});
expect(cachedResponse2).toBeUndefined();
});

let me know if you have any doubts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants