()
| 4 | export const NO_POST_CACHE_MAX_SIZE = 500; |
| 5 | |
| 6 | export const createNoPostCache = () => { |
| 7 | const cache = new Map<string, number>(); |
| 8 | |
| 9 | const has = (url: string, now = Date.now()): boolean => { |
| 10 | const expiresAt = cache.get(url); |
| 11 | if (!expiresAt) { |
| 12 | return false; |
| 13 | } |
| 14 | |
| 15 | if (expiresAt > now) { |
| 16 | return true; |
| 17 | } |
| 18 | |
| 19 | cache.delete(url); |
| 20 | return false; |
| 21 | }; |
| 22 | |
| 23 | const add = (url: string, now = Date.now()): void => { |
| 24 | cache.delete(url); |
| 25 | cache.set(url, now + NO_POST_CACHE_TTL); |
| 26 | if (cache.size > NO_POST_CACHE_MAX_SIZE) { |
| 27 | cache.delete(cache.keys().next().value as string); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | return { has, add }; |
| 32 | }; |
| 33 | |
| 34 | export const noPostCache = createNoPostCache(); |
no outgoing calls
no test coverage detected