mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update cache and ttls interface
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user