|
| 1 | +import { AsyncLocalStorage } from 'node:async_hooks'; |
| 2 | + |
| 3 | +const asyncLocalStorage = new AsyncLocalStorage(); |
| 4 | +const cache = new Map(); |
| 5 | +const cacheFnMap = new Map(); |
| 6 | + |
| 7 | +// revalidate the cache |
| 8 | +function revalidate(key, ...args) { |
| 9 | + cache.delete(key); |
| 10 | + |
| 11 | + const cacheFn = cacheFnMap.get(key); |
| 12 | + if (!cacheFn) return; |
| 13 | + |
| 14 | + return asyncLocalStorage.run( |
| 15 | + { |
| 16 | + tag: key, |
| 17 | + life: cache.get(key)?.life || 0, |
| 18 | + }, |
| 19 | + () => { |
| 20 | + const result = cacheFn(...args); |
| 21 | + |
| 22 | + cache.set(key, { |
| 23 | + value: result, |
| 24 | + time: Date.now(), |
| 25 | + life: asyncLocalStorage.getStore().life || 0, |
| 26 | + }); |
| 27 | + |
| 28 | + return result; |
| 29 | + }, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +// tags the current cached function with a name |
| 34 | +function cacheTag(name) { |
| 35 | + const store = asyncLocalStorage.getStore(); |
| 36 | + if (store) { |
| 37 | + store.tag = name; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// sets the cache life of the current cached function |
| 42 | +function cacheLife(time) { |
| 43 | + const store = asyncLocalStorage.getStore(); |
| 44 | + if (store) { |
| 45 | + store.life = time; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// makes a cached function |
| 50 | +function makeCached(fn) { |
| 51 | + return (...args) => { |
| 52 | + return asyncLocalStorage.run({}, () => { |
| 53 | + const result = fn(...args); |
| 54 | + const store = asyncLocalStorage.getStore(); |
| 55 | + |
| 56 | + if (!store.tag) return result; |
| 57 | + |
| 58 | + const cacheKey = store.tag; |
| 59 | + const cached = cache.get(cacheKey); |
| 60 | + |
| 61 | + cacheFnMap.set(cacheKey, fn); |
| 62 | + |
| 63 | + if (cached && Date.now() - cached.time < cached.life) { |
| 64 | + return cached.value; |
| 65 | + } |
| 66 | + |
| 67 | + cache.set(cacheKey, { |
| 68 | + value: result, |
| 69 | + time: Date.now(), |
| 70 | + life: store.life || 0, |
| 71 | + }); |
| 72 | + |
| 73 | + return result; |
| 74 | + }); |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +// the cached function |
| 79 | +const getData = makeCached((name) => { |
| 80 | + cacheTag(`tag:${name}`); |
| 81 | + cacheLife(1000); |
| 82 | + |
| 83 | + return Math.random(); |
| 84 | +}); |
| 85 | + |
| 86 | +// get the data |
| 87 | +console.log('FOO', getData('foo'), getData('foo')); // should be same |
| 88 | + |
| 89 | +console.log('BAR', getData('bar'), getData('bar')); // should be same but different from above |
| 90 | + |
| 91 | +console.log('FOOBAR', getData('foo'), getData('bar')); // should be different |
| 92 | + |
| 93 | +revalidate('tag:foo'); |
| 94 | + |
| 95 | +console.log('FOO', getData('foo'), getData('foo')); // should be different from above |
0 commit comments