update cache and ttls interface

This commit is contained in:
2025-01-07 20:32:54 -08:00
parent 4cfff7fcca
commit effd3928f0

View File

@@ -11,15 +11,16 @@ export interface ApiBasedRepositoryCache {
stopsBySystemId?: { stopsBySystemId?: {
[systemId: string]: IStop[], [systemId: string]: IStop[],
}, },
shuttlesByShuttleId?: { shuttleByShuttleId?: {
[shuttleId: string]: IShuttle[], [shuttleId: string]: IShuttle,
}, },
// To speed things up, implement caches for other data later // To speed things up, implement caches for other data later
} }
export interface ApiBasedRepositoryMillisecondTTLs { export interface ApiBasedRepositoryMillisecondTTLs {
etasForShuttleId: number, etasForShuttleId?: number,
etasForStopId: number, etasForStopId?: number,
shuttleByShuttleId?: number,
} }
const emptyCache: ApiBasedRepositoryCache = { const emptyCache: ApiBasedRepositoryCache = {
@@ -33,10 +34,13 @@ const defaultTtls: ApiBasedRepositoryMillisecondTTLs = {
} }
export class ApiBasedRepository implements GetterRepository { export class ApiBasedRepository implements GetterRepository {
private cache: ApiBasedRepositoryCache;
constructor( constructor(
private initialCache: ApiBasedRepositoryCache | undefined = emptyCache, initialCache: ApiBasedRepositoryCache | undefined = emptyCache,
private ttls: ApiBasedRepositoryMillisecondTTLs = defaultTtls, private ttls: ApiBasedRepositoryMillisecondTTLs = defaultTtls,
) { ) {
this.cache = initialCache;
} }
/** /**
@@ -60,8 +64,8 @@ export class ApiBasedRepository implements GetterRepository {
return null; return null;
} }
if (this.initialCache?.etasForStopId !== undefined) { if (this.cache?.etasForStopId !== undefined) {
const etas = this.initialCache.etasForStopId[systemId]; const etas = this.cache.etasForStopId[systemId];
const foundEta = etas.find((eta) => eta.stopId === stopId); const foundEta = etas.find((eta) => eta.stopId === stopId);
if (foundEta) { if (foundEta) {
return foundEta; return foundEta;