fansunited-data-layer 0.13.0 → 0.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/fansunited/constants.js +5 -0
- package/api/fansunited/constants.js.map +1 -0
- package/api/fansunited/football/competition/index.js +21 -0
- package/api/fansunited/football/competition/index.js.map +1 -0
- package/api/fansunited/football/competition/transformer.js +41 -0
- package/api/fansunited/football/competition/transformer.js.map +1 -0
- package/api/fansunited/football/competitions/index.js +56 -0
- package/api/fansunited/football/competitions/index.js.map +1 -0
- package/api/fansunited/football/competitions/transformer.js +68 -0
- package/api/fansunited/football/competitions/transformer.js.map +1 -0
- package/api/fansunited/football/http.js +7 -0
- package/api/fansunited/football/http.js.map +1 -0
- package/api/fansunited/football/matches/index.js +132 -0
- package/api/fansunited/football/matches/index.js.map +1 -0
- package/api/fansunited/football/matches/transformer.js +148 -0
- package/api/fansunited/football/matches/transformer.js.map +1 -0
- package/api/fansunited/football/players/index.js +60 -0
- package/api/fansunited/football/players/index.js.map +1 -0
- package/api/fansunited/football/players/transformer.js +85 -0
- package/api/fansunited/football/players/transformer.js.map +1 -0
- package/api/fansunited/football/search/index.js +64 -0
- package/api/fansunited/football/search/index.js.map +1 -0
- package/api/fansunited/football/search/transformer.js +114 -0
- package/api/fansunited/football/search/transformer.js.map +1 -0
- package/api/fansunited/football/teams/index.js +66 -0
- package/api/fansunited/football/teams/index.js.map +1 -0
- package/api/fansunited/football/teams/transformer.js +53 -0
- package/api/fansunited/football/teams/transformer.js.map +1 -0
- package/api/fansunited/http.d.ts.map +1 -1
- package/api/fansunited/http.js +70 -0
- package/api/fansunited/http.js.map +1 -0
- package/api/fansunited/search/constants.js +5 -0
- package/api/fansunited/search/constants.js.map +1 -0
- package/api/fansunited/search/http.js +7 -0
- package/api/fansunited/search/http.js.map +1 -0
- package/api/fansunited/search/index.js +308 -0
- package/api/fansunited/search/index.js.map +1 -0
- package/api/fansunited/search/transformer.js +374 -0
- package/api/fansunited/search/transformer.js.map +1 -0
- package/api/fansunited-sdk/loyalty/matches.js +21 -0
- package/api/fansunited-sdk/loyalty/matches.js.map +1 -0
- package/api/fansunited-sdk/loyalty/template.js +52 -0
- package/api/fansunited-sdk/loyalty/template.js.map +1 -0
- package/api/fansunited-sdk/odds/matches.js +109 -0
- package/api/fansunited-sdk/odds/matches.js.map +1 -0
- package/api/sportal365-sports/basketball/games/index.js +32 -0
- package/api/sportal365-sports/basketball/games/index.js.map +1 -0
- package/api/sportal365-sports/basketball/games/transformers/game.js +137 -0
- package/api/sportal365-sports/basketball/games/transformers/game.js.map +1 -0
- package/api/sportal365-sports/basketball/http.js +7 -0
- package/api/sportal365-sports/basketball/http.js.map +1 -0
- package/api/sportal365-sports/constants.js +15 -0
- package/api/sportal365-sports/constants.js.map +1 -0
- package/api/sportal365-sports/football/competitions/index.js +52 -0
- package/api/sportal365-sports/football/competitions/index.js.map +1 -0
- package/api/sportal365-sports/football/competitions/utils.js +92 -0
- package/api/sportal365-sports/football/competitions/utils.js.map +1 -0
- package/api/sportal365-sports/football/http.js +7 -0
- package/api/sportal365-sports/football/http.js.map +1 -0
- package/api/sportal365-sports/football/matches/index.js +206 -0
- package/api/sportal365-sports/football/matches/index.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/commentary.js +27 -0
- package/api/sportal365-sports/football/matches/transformers/commentary.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/lineup.js +74 -0
- package/api/sportal365-sports/football/matches/transformers/lineup.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/match-event.js +60 -0
- package/api/sportal365-sports/football/matches/transformers/match-event.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/match.js +195 -0
- package/api/sportal365-sports/football/matches/transformers/match.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/odds.js +11 -0
- package/api/sportal365-sports/football/matches/transformers/odds.js.map +1 -0
- package/api/sportal365-sports/football/matches/transformers/statistics.js +41 -0
- package/api/sportal365-sports/football/matches/transformers/statistics.js.map +1 -0
- package/api/sportal365-sports/football/search/index.js +42 -0
- package/api/sportal365-sports/football/search/index.js.map +1 -0
- package/api/sportal365-sports/football/search/search.transformer.js +166 -0
- package/api/sportal365-sports/football/search/search.transformer.js.map +1 -0
- package/api/sportal365-sports/football/standings/index.js +28 -0
- package/api/sportal365-sports/football/standings/index.js.map +1 -0
- package/api/sportal365-sports/football/standings/standing.transformer.js +107 -0
- package/api/sportal365-sports/football/standings/standing.transformer.js.map +1 -0
- package/api/sportal365-sports/football/statistics/index.js +154 -0
- package/api/sportal365-sports/football/statistics/index.js.map +1 -0
- package/api/sportal365-sports/football/statistics/player-career.transformer.js +77 -0
- package/api/sportal365-sports/football/statistics/player-career.transformer.js.map +1 -0
- package/api/sportal365-sports/football/statistics/player-recent.transformer.js +54 -0
- package/api/sportal365-sports/football/statistics/player-recent.transformer.js.map +1 -0
- package/api/sportal365-sports/football/statistics/player-season.transformer.js +147 -0
- package/api/sportal365-sports/football/statistics/player-season.transformer.js.map +1 -0
- package/api/sportal365-sports/football/teams/index.js +73 -0
- package/api/sportal365-sports/football/teams/index.js.map +1 -0
- package/api/sportal365-sports/football/teams/utils.js +196 -0
- package/api/sportal365-sports/football/teams/utils.js.map +1 -0
- package/api/sportal365-sports/http.js +58 -0
- package/api/sportal365-sports/http.js.map +1 -0
- package/api/sportal365-sports/search/http.js +7 -0
- package/api/sportal365-sports/search/http.js.map +1 -0
- package/api/sportal365-sports/search/index.js +61 -0
- package/api/sportal365-sports/search/index.js.map +1 -0
- package/api/sportal365-sports/search/search.transformer.js +254 -0
- package/api/sportal365-sports/search/search.transformer.js.map +1 -0
- package/api/sportal365-sports/shared/odds.transformer.js +65 -0
- package/api/sportal365-sports/shared/odds.transformer.js.map +1 -0
- package/api/sportal365-sports/shared/providerRef.helper.js +31 -0
- package/api/sportal365-sports/shared/providerRef.helper.js.map +1 -0
- package/api/sportal365-sports/standings/http.js +7 -0
- package/api/sportal365-sports/standings/http.js.map +1 -0
- package/api/sportal365-sports/standings/index.js +29 -0
- package/api/sportal365-sports/standings/index.js.map +1 -0
- package/api/sportal365-sports/standings/standing.transformer.js +185 -0
- package/api/sportal365-sports/standings/standing.transformer.js.map +1 -0
- package/api/sportal365-sports/statistics/http.js +7 -0
- package/api/sportal365-sports/statistics/http.js.map +1 -0
- package/api/sportal365-sports/statistics/index.js +25 -0
- package/api/sportal365-sports/statistics/index.js.map +1 -0
- package/api/sportal365-sports/statistics/team-stats.transformer.js +16 -0
- package/api/sportal365-sports/statistics/team-stats.transformer.js.map +1 -0
- package/api/sportal365-sports/tennis/http.js +7 -0
- package/api/sportal365-sports/tennis/http.js.map +1 -0
- package/api/sportal365-sports/tennis/matches/index.js +34 -0
- package/api/sportal365-sports/tennis/matches/index.js.map +1 -0
- package/api/sportal365-sports/tennis/matches/transformers/match.js +193 -0
- package/api/sportal365-sports/tennis/matches/transformers/match.js.map +1 -0
- package/cache/cache-manager.js +136 -0
- package/cache/cache-manager.js.map +1 -0
- package/cache/memory-store.js +29 -0
- package/cache/memory-store.js.map +1 -0
- package/client.js +38 -12337
- package/client.js.map +1 -0
- package/config/index.js +20 -0
- package/config/index.js.map +1 -0
- package/fansunited-data-layer.js +146 -3553
- package/fansunited-data-layer.js.map +1 -0
- package/helpers/competition.helpers.js +11 -0
- package/helpers/competition.helpers.js.map +1 -0
- package/helpers/player.helpers.js +165 -0
- package/helpers/player.helpers.js.map +1 -0
- package/helpers/player.hooks.js +34 -0
- package/helpers/player.hooks.js.map +1 -0
- package/helpers/team.helpers.js +216 -0
- package/helpers/team.helpers.js.map +1 -0
- package/package.json +6 -8
- package/providers/competition/hooks/useCompetitionStats.js +10 -0
- package/providers/competition/hooks/useCompetitionStats.js.map +1 -0
- package/providers/competition/hooks/useGoalDistribution.js +23 -0
- package/providers/competition/hooks/useGoalDistribution.js.map +1 -0
- package/providers/competition/hooks/useMatchHelpers.js +85 -0
- package/providers/competition/hooks/useMatchHelpers.js.map +1 -0
- package/providers/competition/hooks/useStandingsCalculations.js +89 -0
- package/providers/competition/hooks/useStandingsCalculations.js.map +1 -0
- package/providers/competition/hooks/useStandingsHelpers.js +61 -0
- package/providers/competition/hooks/useStandingsHelpers.js.map +1 -0
- package/providers/competition/hooks/useTeamPosition.js +15 -0
- package/providers/competition/hooks/useTeamPosition.js.map +1 -0
- package/providers/competition/hooks/useTeamResultsTable.js +86 -0
- package/providers/competition/hooks/useTeamResultsTable.js.map +1 -0
- package/providers/competition/utils/competitionStats.js +109 -0
- package/providers/competition/utils/competitionStats.js.map +1 -0
- package/providers/competition/utils/goalDistribution.js +64 -0
- package/providers/competition/utils/goalDistribution.js.map +1 -0
- package/providers/competition/utils/standingsCalculations.js +152 -0
- package/providers/competition/utils/standingsCalculations.js.map +1 -0
- package/providers/competition.context.js +14 -0
- package/providers/competition.context.js.map +1 -0
- package/providers/competition.provider.js +187 -0
- package/providers/competition.provider.js.map +1 -0
- package/providers/fansunited-config.context.js +6 -0
- package/providers/fansunited-config.context.js.map +1 -0
- package/providers/fansunited-config.hooks.js +20 -0
- package/providers/fansunited-config.hooks.js.map +1 -0
- package/providers/fansunited-config.provider.js +9 -0
- package/providers/fansunited-config.provider.js.map +1 -0
- package/providers/fansunited-sdk.hook.js +50 -0
- package/providers/fansunited-sdk.hook.js.map +1 -0
- package/providers/leaderboard/hooks/useMatchHelpers.js +30 -0
- package/providers/leaderboard/hooks/useMatchHelpers.js.map +1 -0
- package/providers/leaderboard/hooks/useTemplateHelpers.js +39 -0
- package/providers/leaderboard/hooks/useTemplateHelpers.js.map +1 -0
- package/providers/leaderboard.context.js +14 -0
- package/providers/leaderboard.context.js.map +1 -0
- package/providers/leaderboard.provider.js +98 -0
- package/providers/leaderboard.provider.js.map +1 -0
- package/providers/match/hooks/useBatchMatchOdds.js +72 -0
- package/providers/match/hooks/useBatchMatchOdds.js.map +1 -0
- package/providers/match/hooks/useEventHelpers.js +46 -0
- package/providers/match/hooks/useEventHelpers.js.map +1 -0
- package/providers/match/hooks/useHeadToHeadHelpers.js +52 -0
- package/providers/match/hooks/useHeadToHeadHelpers.js.map +1 -0
- package/providers/match/hooks/useLineupHelpers.js +53 -0
- package/providers/match/hooks/useLineupHelpers.js.map +1 -0
- package/providers/match/hooks/useMatchStandingsCalculations.js +332 -0
- package/providers/match/hooks/useMatchStandingsCalculations.js.map +1 -0
- package/providers/match/hooks/useMatchStatus.js +37 -0
- package/providers/match/hooks/useMatchStatus.js.map +1 -0
- package/providers/match/hooks/useOddsHelpers.js +54 -0
- package/providers/match/hooks/useOddsHelpers.js.map +1 -0
- package/providers/match/hooks/useScoreHelpers.js +34 -0
- package/providers/match/hooks/useScoreHelpers.js.map +1 -0
- package/providers/match/hooks/useStandingsHelpers.js +53 -0
- package/providers/match/hooks/useStandingsHelpers.js.map +1 -0
- package/providers/match/hooks/useStatisticsHelpers.js +40 -0
- package/providers/match/hooks/useStatisticsHelpers.js.map +1 -0
- package/providers/match/hooks/useTeamHelpers.js +38 -0
- package/providers/match/hooks/useTeamHelpers.js.map +1 -0
- package/providers/match/hooks/useTeamStatsHelpers.js +141 -0
- package/providers/match/hooks/useTeamStatsHelpers.js.map +1 -0
- package/providers/match.context.js +14 -0
- package/providers/match.context.js.map +1 -0
- package/providers/match.provider.js +176 -0
- package/providers/match.provider.js.map +1 -0
- package/providers/team/hooks/useFormStats.js +116 -0
- package/providers/team/hooks/useFormStats.js.map +1 -0
- package/providers/team/hooks/useMatchHelpers.js +46 -0
- package/providers/team/hooks/useMatchHelpers.js.map +1 -0
- package/providers/team/hooks/useSquadHelpers.js +67 -0
- package/providers/team/hooks/useSquadHelpers.js.map +1 -0
- package/providers/team.context.js +14 -0
- package/providers/team.context.js.map +1 -0
- package/providers/team.provider.js +98 -0
- package/providers/team.provider.js.map +1 -0
- package/utilities/stats/core/helpers.js +105 -0
- package/utilities/stats/core/helpers.js.map +1 -0
- package/utilities/stats/core/types.js +5 -0
- package/utilities/stats/core/types.js.map +1 -0
- package/utilities/stats/match/headToHead.js +83 -0
- package/utilities/stats/match/headToHead.js.map +1 -0
- package/utilities/stats/match/homeVsAway.js +140 -0
- package/utilities/stats/match/homeVsAway.js.map +1 -0
- package/utilities/stats/match/overUnder.js +91 -0
- package/utilities/stats/match/overUnder.js.map +1 -0
- package/utilities/stats/match/result.js +21 -0
- package/utilities/stats/match/result.js.map +1 -0
- package/utilities/stats/team/commonOpponents.js +77 -0
- package/utilities/stats/team/commonOpponents.js.map +1 -0
- package/utilities/stats/team/goalStats.js +50 -0
- package/utilities/stats/team/goalStats.js.map +1 -0
- package/utilities/stats/team/streaks.js +183 -0
- package/utilities/stats/team/streaks.js.map +1 -0
- package/client.cjs +0 -8
- package/fansunited-data-layer.cjs +0 -1
- package/matches-BvHU-bP0.js +0 -1795
- package/matches-Ci0Ci_Mw.cjs +0 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { memoryStore } from "./memory-store.js";
|
|
2
|
+
let l2 = null;
|
|
3
|
+
function initL2(store) {
|
|
4
|
+
l2 = store;
|
|
5
|
+
}
|
|
6
|
+
const ENTITY_TTL_CONFIG = {
|
|
7
|
+
competitions: { staleTTL: 24 * 3600, maxTTL: 72 * 3600 },
|
|
8
|
+
teams: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
|
|
9
|
+
athletes: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
|
|
10
|
+
venues: { staleTTL: 72 * 3600, maxTTL: 168 * 3600 },
|
|
11
|
+
countries: { staleTTL: 96 * 3600, maxTTL: 168 * 3600 },
|
|
12
|
+
coaches: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },
|
|
13
|
+
search: { staleTTL: 300, maxTTL: 1800 }
|
|
14
|
+
};
|
|
15
|
+
const refreshing = /* @__PURE__ */ new Set();
|
|
16
|
+
function getConfig(entity) {
|
|
17
|
+
const config = ENTITY_TTL_CONFIG[entity];
|
|
18
|
+
if (!config) {
|
|
19
|
+
throw new Error(`Unknown entity type: ${entity}`);
|
|
20
|
+
}
|
|
21
|
+
return config;
|
|
22
|
+
}
|
|
23
|
+
function isFresh(entry, staleTTL) {
|
|
24
|
+
return Date.now() - entry.storedAt < staleTTL * 1e3;
|
|
25
|
+
}
|
|
26
|
+
function isExpired(entry, maxTTL) {
|
|
27
|
+
return Date.now() - entry.storedAt >= maxTTL * 1e3;
|
|
28
|
+
}
|
|
29
|
+
function backgroundRefresh(key, entity, fetcher) {
|
|
30
|
+
if (refreshing.has(key)) return;
|
|
31
|
+
refreshing.add(key);
|
|
32
|
+
fetcher().then((data) => {
|
|
33
|
+
memoryStore.set(key, data);
|
|
34
|
+
if (l2?.isInitialized) {
|
|
35
|
+
l2.set(key, entity, data);
|
|
36
|
+
}
|
|
37
|
+
}).catch((err) => {
|
|
38
|
+
console.error(`[cache] Background refresh failed for ${key}:`, err);
|
|
39
|
+
}).finally(() => {
|
|
40
|
+
refreshing.delete(key);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function cached(key, entity, fetcher) {
|
|
44
|
+
const { staleTTL, maxTTL } = getConfig(entity);
|
|
45
|
+
const l1 = memoryStore.get(key);
|
|
46
|
+
if (l1) {
|
|
47
|
+
if (isFresh(l1, staleTTL)) {
|
|
48
|
+
return l1.data;
|
|
49
|
+
}
|
|
50
|
+
if (!isExpired(l1, maxTTL)) {
|
|
51
|
+
backgroundRefresh(key, entity, fetcher);
|
|
52
|
+
return l1.data;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (l2?.isInitialized) {
|
|
56
|
+
const l2Entry = l2.get(key);
|
|
57
|
+
if (l2Entry && !isExpired(l2Entry, maxTTL)) {
|
|
58
|
+
memoryStore.set(key, l2Entry.data);
|
|
59
|
+
if (isFresh(l2Entry, staleTTL)) {
|
|
60
|
+
return l2Entry.data;
|
|
61
|
+
}
|
|
62
|
+
backgroundRefresh(key, entity, fetcher);
|
|
63
|
+
return l2Entry.data;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const data = await fetcher();
|
|
67
|
+
memoryStore.set(key, data);
|
|
68
|
+
if (l2?.isInitialized) {
|
|
69
|
+
l2.set(key, entity, data);
|
|
70
|
+
}
|
|
71
|
+
return data;
|
|
72
|
+
}
|
|
73
|
+
async function cachedBatch(ids, entity, batchFetcher, keyFn) {
|
|
74
|
+
const { staleTTL, maxTTL } = getConfig(entity);
|
|
75
|
+
const results = /* @__PURE__ */ new Map();
|
|
76
|
+
const missingIds = [];
|
|
77
|
+
for (const id of ids) {
|
|
78
|
+
const key = keyFn(id);
|
|
79
|
+
let found = false;
|
|
80
|
+
const l1 = memoryStore.get(key);
|
|
81
|
+
if (l1 && !isExpired(l1, maxTTL)) {
|
|
82
|
+
results.set(id, l1.data);
|
|
83
|
+
if (!isFresh(l1, staleTTL)) {
|
|
84
|
+
backgroundRefresh(key, entity, async () => {
|
|
85
|
+
const fetched = await batchFetcher([id]);
|
|
86
|
+
const item = fetched.get(id);
|
|
87
|
+
if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);
|
|
88
|
+
return item;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
found = true;
|
|
92
|
+
}
|
|
93
|
+
if (!found && l2?.isInitialized) {
|
|
94
|
+
const l2Entry = l2.get(key);
|
|
95
|
+
if (l2Entry && !isExpired(l2Entry, maxTTL)) {
|
|
96
|
+
memoryStore.set(key, l2Entry.data);
|
|
97
|
+
results.set(id, l2Entry.data);
|
|
98
|
+
if (!isFresh(l2Entry, staleTTL)) {
|
|
99
|
+
backgroundRefresh(key, entity, async () => {
|
|
100
|
+
const fetched = await batchFetcher([id]);
|
|
101
|
+
const item = fetched.get(id);
|
|
102
|
+
if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);
|
|
103
|
+
return item;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
found = true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!found) {
|
|
110
|
+
missingIds.push(id);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (missingIds.length > 0) {
|
|
114
|
+
const fetched = await batchFetcher(missingIds);
|
|
115
|
+
for (const [id, data] of fetched) {
|
|
116
|
+
const key = keyFn(id);
|
|
117
|
+
memoryStore.set(key, data);
|
|
118
|
+
if (l2?.isInitialized) {
|
|
119
|
+
l2.set(key, entity, data);
|
|
120
|
+
}
|
|
121
|
+
results.set(id, data);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
function invalidate(key) {
|
|
127
|
+
memoryStore.delete(key);
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
ENTITY_TTL_CONFIG,
|
|
131
|
+
cached,
|
|
132
|
+
cachedBatch,
|
|
133
|
+
initL2,
|
|
134
|
+
invalidate
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=cache-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-manager.js","sources":["../../src/lib/cache/cache-manager.ts"],"sourcesContent":["/**\n * Cache Manager - Orchestrates L1 (memory) + optional L2 with SWR pattern\n *\n * L2 (SQLite) is never statically imported here — it's injected at runtime\n * via initL2() so that browser bundles don't pull in better-sqlite3.\n */\n\nimport { memoryStore } from \"./memory-store\";\nimport type { CacheEntry, EntityCacheConfig, EntityType } from \"./types\";\n\n/** Interface that L2 stores must satisfy */\nexport interface L2Store {\n get isInitialized(): boolean;\n get<T>(key: string): CacheEntry<T> | undefined;\n set(key: string, entity: string, data: unknown): void;\n setMany(entries: { key: string; entity: string; data: unknown }[]): void;\n cleanup(entity: string, maxTTLSeconds: number): number;\n clear(): void;\n}\n\n/** The active L2 store — null until initL2() is called */\nlet l2: L2Store | null = null;\n\n/**\n * Initialize L2 cache layer. Call on server startup.\n * Client-side apps skip this — L2 is never loaded.\n */\nexport function initL2(store: L2Store): void {\n l2 = store;\n}\n\n/** Get the L2 store (if initialized) */\nexport function getL2(): L2Store | null {\n return l2;\n}\n\nconst ENTITY_TTL_CONFIG: Record<EntityType, EntityCacheConfig> = {\n competitions: { staleTTL: 24 * 3600, maxTTL: 72 * 3600 },\n teams: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n athletes: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n venues: { staleTTL: 72 * 3600, maxTTL: 168 * 3600 },\n countries: { staleTTL: 96 * 3600, maxTTL: 168 * 3600 },\n coaches: { staleTTL: 24 * 3600, maxTTL: 48 * 3600 },\n search: { staleTTL: 300, maxTTL: 1800 },\n};\n\n/** Set of keys currently being refreshed in the background (thundering herd prevention) */\nconst refreshing = new Set<string>();\n\nfunction getConfig(entity: EntityType): EntityCacheConfig {\n const config = ENTITY_TTL_CONFIG[entity];\n if (!config) {\n throw new Error(`Unknown entity type: ${entity}`);\n }\n return config;\n}\n\nfunction isFresh(entry: CacheEntry<unknown>, staleTTL: number): boolean {\n return Date.now() - entry.storedAt < staleTTL * 1000;\n}\n\nfunction isExpired(entry: CacheEntry<unknown>, maxTTL: number): boolean {\n return Date.now() - entry.storedAt >= maxTTL * 1000;\n}\n\nfunction backgroundRefresh<T>(key: string, entity: EntityType, fetcher: () => Promise<T>): void {\n if (refreshing.has(key)) return;\n refreshing.add(key);\n\n fetcher()\n .then((data) => {\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n })\n .catch((err) => {\n console.error(`[cache] Background refresh failed for ${key}:`, err);\n })\n .finally(() => {\n refreshing.delete(key);\n });\n}\n\n/**\n * Single-key cache-through with SWR (Stale-While-Revalidate)\n */\nexport async function cached<T>(key: string, entity: EntityType, fetcher: () => Promise<T>): Promise<T> {\n const { staleTTL, maxTTL } = getConfig(entity);\n\n // L1 check\n const l1 = memoryStore.get<T>(key);\n if (l1) {\n if (isFresh(l1, staleTTL)) {\n return l1.data;\n }\n if (!isExpired(l1, maxTTL)) {\n backgroundRefresh(key, entity, fetcher);\n return l1.data;\n }\n }\n\n // L2 check\n if (l2?.isInitialized) {\n const l2Entry = l2.get<T>(key);\n if (l2Entry && !isExpired(l2Entry, maxTTL)) {\n // Promote to L1\n memoryStore.set(key, l2Entry.data);\n if (isFresh(l2Entry, staleTTL)) {\n return l2Entry.data;\n }\n backgroundRefresh(key, entity, fetcher);\n return l2Entry.data;\n }\n }\n\n // Full miss — await fetcher\n const data = await fetcher();\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n return data;\n}\n\n/**\n * Smart batch cache — only fetches uncached IDs from the API\n */\nexport async function cachedBatch<T>(\n ids: string[],\n entity: EntityType,\n batchFetcher: (missingIds: string[]) => Promise<Map<string, T>>,\n keyFn: (id: string) => string\n): Promise<Map<string, T>> {\n const { staleTTL, maxTTL } = getConfig(entity);\n const results = new Map<string, T>();\n const missingIds: string[] = [];\n\n for (const id of ids) {\n const key = keyFn(id);\n let found = false;\n\n // L1 check\n const l1 = memoryStore.get<T>(key);\n if (l1 && !isExpired(l1, maxTTL)) {\n results.set(id, l1.data);\n if (!isFresh(l1, staleTTL)) {\n backgroundRefresh(key, entity, async () => {\n const fetched = await batchFetcher([id]);\n const item = fetched.get(id);\n if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);\n return item;\n });\n }\n found = true;\n }\n\n // L2 check\n if (!found && l2?.isInitialized) {\n const l2Entry = l2.get<T>(key);\n if (l2Entry && !isExpired(l2Entry, maxTTL)) {\n memoryStore.set(key, l2Entry.data);\n results.set(id, l2Entry.data);\n if (!isFresh(l2Entry, staleTTL)) {\n backgroundRefresh(key, entity, async () => {\n const fetched = await batchFetcher([id]);\n const item = fetched.get(id);\n if (!item) throw new Error(`Batch fetcher did not return ID: ${id}`);\n return item;\n });\n }\n found = true;\n }\n }\n\n if (!found) {\n missingIds.push(id);\n }\n }\n\n // Fetch missing IDs in a single batch\n if (missingIds.length > 0) {\n const fetched = await batchFetcher(missingIds);\n for (const [id, data] of fetched) {\n const key = keyFn(id);\n memoryStore.set(key, data);\n if (l2?.isInitialized) {\n l2.set(key, entity, data);\n }\n results.set(id, data);\n }\n }\n\n return results;\n}\n\n/**\n * Remove an entry from both cache layers\n */\nexport function invalidate(key: string): void {\n memoryStore.delete(key);\n}\n\n// Export for testing\nexport { ENTITY_TTL_CONFIG };\n"],"names":[],"mappings":";AAqBA,IAAI,KAAqB;AAMlB,SAAS,OAAO,OAAsB;AACzC,OAAK;AACT;AAOA,MAAM,oBAA2D;AAAA,EAC7D,cAAc,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAClD,OAAO,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAC3C,UAAU,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAC9C,QAAQ,EAAE,UAAU,KAAK,MAAM,QAAQ,MAAM,KAAA;AAAA,EAC7C,WAAW,EAAE,UAAU,KAAK,MAAM,QAAQ,MAAM,KAAA;AAAA,EAChD,SAAS,EAAE,UAAU,KAAK,MAAM,QAAQ,KAAK,KAAA;AAAA,EAC7C,QAAQ,EAAE,UAAU,KAAK,QAAQ,KAAA;AACrC;AAGA,MAAM,iCAAiB,IAAA;AAEvB,SAAS,UAAU,QAAuC;AACtD,QAAM,SAAS,kBAAkB,MAAM;AACvC,MAAI,CAAC,QAAQ;AACT,UAAM,IAAI,MAAM,wBAAwB,MAAM,EAAE;AAAA,EACpD;AACA,SAAO;AACX;AAEA,SAAS,QAAQ,OAA4B,UAA2B;AACpE,SAAO,KAAK,IAAA,IAAQ,MAAM,WAAW,WAAW;AACpD;AAEA,SAAS,UAAU,OAA4B,QAAyB;AACpE,SAAO,KAAK,IAAA,IAAQ,MAAM,YAAY,SAAS;AACnD;AAEA,SAAS,kBAAqB,KAAa,QAAoB,SAAiC;AAC5F,MAAI,WAAW,IAAI,GAAG,EAAG;AACzB,aAAW,IAAI,GAAG;AAElB,UAAA,EACK,KAAK,CAAC,SAAS;AACZ,gBAAY,IAAI,KAAK,IAAI;AACzB,QAAI,IAAI,eAAe;AACnB,SAAG,IAA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IACxB;AAAA,EACJ;AAEA,SAAO;AACX;AAKO,SAAS,WAAW,KAAmB;AAC1C,cAAY,OAAO,GAAG;AAC1B;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { LRUCache } from "lru-cache";
|
|
2
|
+
const MAX_SIZE_BYTES = 50 * 1024 * 1024;
|
|
3
|
+
const cache = new LRUCache({
|
|
4
|
+
maxSize: MAX_SIZE_BYTES,
|
|
5
|
+
sizeCalculation: (entry) => {
|
|
6
|
+
return JSON.stringify(entry.data).length;
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
const memoryStore = {
|
|
10
|
+
get(key) {
|
|
11
|
+
return cache.get(key);
|
|
12
|
+
},
|
|
13
|
+
set(key, data) {
|
|
14
|
+
cache.set(key, { data, storedAt: Date.now() });
|
|
15
|
+
},
|
|
16
|
+
delete(key) {
|
|
17
|
+
cache.delete(key);
|
|
18
|
+
},
|
|
19
|
+
clear() {
|
|
20
|
+
cache.clear();
|
|
21
|
+
},
|
|
22
|
+
get size() {
|
|
23
|
+
return cache.size;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export {
|
|
27
|
+
memoryStore
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=memory-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.js","sources":["../../src/lib/cache/memory-store.ts"],"sourcesContent":["/**\n * L1 Cache - LRU in-memory store\n */\n\nimport { LRUCache } from \"lru-cache\";\nimport type { CacheEntry } from \"./types\";\n\nconst MAX_SIZE_BYTES = 50 * 1024 * 1024; // 50MB\n\nconst cache = new LRUCache<string, CacheEntry<unknown>>({\n maxSize: MAX_SIZE_BYTES,\n sizeCalculation: (entry) => {\n return JSON.stringify(entry.data).length;\n },\n});\n\nexport const memoryStore = {\n get<T>(key: string): CacheEntry<T> | undefined {\n return cache.get(key) as CacheEntry<T> | undefined;\n },\n\n set<T>(key: string, data: T): void {\n cache.set(key, { data, storedAt: Date.now() });\n },\n\n delete(key: string): void {\n cache.delete(key);\n },\n\n clear(): void {\n cache.clear();\n },\n\n get size(): number {\n return cache.size;\n },\n};\n"],"names":[],"mappings":";AAOA,MAAM,iBAAiB,KAAK,OAAO;AAEnC,MAAM,QAAQ,IAAI,SAAsC;AAAA,EACpD,SAAS;AAAA,EACT,iBAAiB,CAAC,UAAU;AACxB,WAAO,KAAK,UAAU,MAAM,IAAI,EAAE;AAAA,EACtC;AACJ,CAAC;AAEM,MAAM,cAAc;AAAA,EACvB,IAAO,KAAwC;AAC3C,WAAO,MAAM,IAAI,GAAG;AAAA,EACxB;AAAA,EAEA,IAAO,KAAa,MAAe;AAC/B,UAAM,IAAI,KAAK,EAAE,MAAM,UAAU,KAAK,IAAA,GAAO;AAAA,EACjD;AAAA,EAEA,OAAO,KAAmB;AACtB,UAAM,OAAO,GAAG;AAAA,EACpB;AAAA,EAEA,QAAc;AACV,UAAM,MAAA;AAAA,EACV;AAAA,EAEA,IAAI,OAAe;AACf,WAAO,MAAM;AAAA,EACjB;AACJ;"}
|