From effd3928f03407ab6a7e57ae9eddb0f48b77d42f Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 7 Jan 2025 20:32:54 -0800 Subject: [PATCH] update cache and ttls interface --- src/repositories/ApiBasedRepository.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index bf41eb6..3b49aac 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -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;