Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 2x 2x 2x 3x 2x 3x 3x 3x 12x 12x 6x 6x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x | import {
getShuttleSchedule,
ShuttleScheduleResponse,
} from "../api/shuttleScheduleApi";
import type { CampusKey } from "../data/ShuttleSchedule";
import cacheService from "./cacheService";
const CACHE_NAMESPACE = "shuttle_schedule";
const CACHE_KEY = "cache";
const CACHE_MAX_AGE_MS = 4 * 60 * 60 * 1000; // 4 hours
export type DeparturesByDay = {
monThu: Record<CampusKey, string[]>;
friday: Record<CampusKey, string[]>;
};
async function getCachedSchedule(): Promise<ShuttleScheduleResponse | null> {
return cacheService.getPersistent<ShuttleScheduleResponse>(
CACHE_NAMESPACE,
CACHE_KEY,
);
}
async function setCachedSchedule(data: ShuttleScheduleResponse): Promise<void> {
await cacheService.setPersistent(
CACHE_NAMESPACE,
CACHE_KEY,
data,
CACHE_MAX_AGE_MS,
);
}
function toDeparturesByDay(response: ShuttleScheduleResponse): DeparturesByDay {
const monThu: Record<CampusKey, string[]> = { sgw: [], loyola: [] };
const friday: Record<CampusKey, string[]> = { sgw: [], loyola: [] };
for (const s of response.schedules) {
const key: CampusKey = s.campus === "SGW" ? "sgw" : "loyola";
if (s.dayType === "weekday") {
monThu[key] = s.departureTimes;
} else {
friday[key] = s.departureTimes;
}
}
return { monThu, friday };
}
export async function getSchedule(): Promise<DeparturesByDay> {
const cached = await getCachedSchedule();
// 1. Cache hit -> return it
if (cached) {
console.log("[SHUTTLE CACHE] HIT - serving cached schedule");
return toDeparturesByDay(cached);
}
// 2. Cache miss/expired -> full fetch
console.log("[SHUTTLE CACHE] MISS - fetching full schedule from API");
const fresh = await getShuttleSchedule();
await setCachedSchedule(fresh);
console.log("[SHUTTLE CACHE] Stored new schedule in cache");
return toDeparturesByDay(fresh);
}
|