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