Last active
April 3, 2020 05:43
-
-
Save SBoudrias/411f03a6311c9375f3d352802cd98cc3 to your computer and use it in GitHub Desktop.
Using JS weakmaps as cache (Node)
This file contains hidden or 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
const cache = new WeakMap(); | |
// This pattern allows us to pass down a single argument (req) down our function chain. | |
// Then each function/class can get their dependencies in this way - and we know it'll only call | |
// our DB/storage once per request. | |
export async function getUserFromReq(req) { | |
if (cache.has(req)) { | |
return cache.get(req); | |
} | |
return await User.get(req.form.session_id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment