generated from trieb-work/public-typescript-npm-package-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat add handler code #3
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24c497f
feat: add handler code
f674f26
feat: add handler code
72251f5
feat: add handler code
2b9b138
fix: lint errors
832c28f
feat: update to next 15 types + feat: add delete sync to deduplicatio…
9e4fab1
fix: rEADME
14afef6
fix: double sync on key expiration
6855303
chore: improve docs
de7a6aa
feat: improve readme and remove logs
79408fb
feat: improve readme and remove logs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CacheHandler } from "next/dist/server/lib/incremental-cache"; | ||
import RedisStringsHandler, { CreateRedisStringsHandlerOptions } from "./RedisStringsHandler"; | ||
|
||
let cachedHandler: RedisStringsHandler; | ||
|
||
export default class CachedHandler implements CacheHandler { | ||
constructor(options: CreateRedisStringsHandlerOptions) { | ||
if (!cachedHandler) { | ||
console.log("created cached handler"); | ||
cachedHandler = new RedisStringsHandler(options); | ||
} | ||
} | ||
get(...args: Parameters<RedisStringsHandler["get"]>): ReturnType<RedisStringsHandler["get"]> { | ||
return cachedHandler.get(...args); | ||
} | ||
set(...args: Parameters<RedisStringsHandler["set"]>): ReturnType<RedisStringsHandler["set"]> { | ||
return cachedHandler.set(...args); | ||
} | ||
revalidateTag(...args: Parameters<RedisStringsHandler["revalidateTag"]>): ReturnType<RedisStringsHandler["revalidateTag"]> { | ||
return cachedHandler.revalidateTag(...args); | ||
} | ||
resetRequestCache(...args: Parameters<RedisStringsHandler["resetRequestCache"]>): ReturnType<RedisStringsHandler["resetRequestCache"]> { | ||
return cachedHandler.resetRequestCache(...args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
let counter = 0; | ||
|
||
export class DeduplicatedRequestHandler< | ||
T extends (...args: [never, never]) => Promise<K>, | ||
K, | ||
> { | ||
private inMemoryDeduplicationCache = new Map<string, Promise<K>>([]); | ||
private cachingTimeMs: number; | ||
private fn: T; | ||
|
||
constructor(fn: T, cachingTimeMs: number) { | ||
this.fn = fn; | ||
this.cachingTimeMs = cachingTimeMs; | ||
console.log('this 1', this); | ||
} | ||
|
||
// Getter to access the cache externally | ||
public get cache(): Map<string, Promise<K>> { | ||
return this.inMemoryDeduplicationCache; | ||
} | ||
|
||
// Method to manually seed a result into the cache | ||
seedRequestReturn(key: string, value: K): void { | ||
const resultPromise = new Promise<K>((res) => res(value)); | ||
this.inMemoryDeduplicationCache.set(key, resultPromise); | ||
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. Right now we have two problems:
|
||
} | ||
|
||
// Method to handle deduplicated requests | ||
deduplicatedFunction = (key: string): T => { | ||
//eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const self = this; | ||
const dedupedFn = async (...args: [never, never]): Promise<K> => { | ||
const cnt = `${key}_${counter++}`; | ||
|
||
// If there's already a pending request with the same key, return it | ||
if ( | ||
self.inMemoryDeduplicationCache && | ||
self.inMemoryDeduplicationCache.has(key) | ||
) { | ||
console.log(`redis get in-mem ${cnt} started`); | ||
console.time(`redis get in-mem ${cnt}`); | ||
const res = await self.inMemoryDeduplicationCache | ||
.get(key)! | ||
.then((v) => structuredClone(v)); | ||
console.timeEnd(`redis get in-mem ${cnt}`); | ||
return res; | ||
} | ||
|
||
// If no pending request, call the original function and store the promise | ||
const promise = self.fn(...args); | ||
self.inMemoryDeduplicationCache.set(key, promise); | ||
|
||
try { | ||
console.log(`redis get origin ${cnt} started`); | ||
console.time(`redis get origin ${cnt}`); | ||
const result = await promise; | ||
console.timeEnd(`redis get origin ${cnt}`); | ||
return structuredClone(result); | ||
} finally { | ||
// Once the promise is resolved/rejected, remove it from the map | ||
setTimeout(() => { | ||
self.inMemoryDeduplicationCache.delete(key); | ||
}, self.cachingTimeMs); | ||
} | ||
}; | ||
return dedupedFn as T; | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache entries which are set in in-memory request will not get deleted by revalidateTag across all nodes (only on local instance). Add keyspace notification to make sure this will get deleted as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This problem does not exist if inMemoryCaching is set to 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If inMemoryCaching is enabled. It will degrade caching consistency from strong to eventual consistency. Add this to the README
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if redis will always serve a get after a set.