| |
|
|
| const MAX_ENTRIES = 16; |
|
|
| const mapDivergence = new Map(); |
| const mapArticles = new Map(); |
| const timelinePayload = new Map(); |
|
|
| function keyTopicTf(topic, newsTimeframe) { |
| return `${topic.trim()}\u0000${newsTimeframe}`; |
| } |
|
|
| function keyArticles(topic, newsTimeframe, iso) { |
| return `${keyTopicTf(topic, newsTimeframe)}\u0000${iso ?? "all"}`; |
| } |
|
|
| function setBounded(map, key, value) { |
| if (map.has(key)) { |
| map.delete(key); |
| } else if (map.size >= MAX_ENTRIES) { |
| const oldest = map.keys().next().value; |
| map.delete(oldest); |
| } |
| map.set(key, value); |
| } |
|
|
| export function getCachedMapDivergence(topic, newsTimeframe) { |
| return mapDivergence.get(keyTopicTf(topic, newsTimeframe)); |
| } |
|
|
| export function setCachedMapDivergence(topic, newsTimeframe, data) { |
| setBounded(mapDivergence, keyTopicTf(topic, newsTimeframe), data); |
| } |
|
|
| export function getCachedMapArticles(topic, newsTimeframe, iso) { |
| return mapArticles.get(keyArticles(topic, newsTimeframe, iso)); |
| } |
|
|
| export function setCachedMapArticles(topic, newsTimeframe, iso, data) { |
| setBounded(mapArticles, keyArticles(topic, newsTimeframe, iso), data); |
| } |
|
|
| export function getCachedTimeline(topic, newsTimeframe) { |
| return timelinePayload.get(keyTopicTf(topic, newsTimeframe)); |
| } |
|
|
| export function setCachedTimeline(topic, newsTimeframe, data) { |
| setBounded(timelinePayload, keyTopicTf(topic, newsTimeframe), data); |
| } |
|
|