diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index b4751c6..42ecbfd 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -82,20 +82,35 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt return super.emit(event, ...args); } - // Helper methods for Redis key generation - private createStopKey = (stopId: string) => `shuttle:stop:${stopId}`; - private createRouteKey = (routeId: string) => `shuttle:route:${routeId}`; - private createShuttleKey = (shuttleId: string) => `shuttle:shuttle:${shuttleId}`; - private createOrderedStopKey = (routeId: string, stopId: string) => `shuttle:orderedstop:${routeId}:${stopId}`; - private createShuttleLastStopKey = (shuttleId: string) => `shuttle:laststop:${shuttleId}`; - private createHistoricalEtaTimeSeriesKey = (routeId: string, fromStopId: string, toStopId: string) => { - return `shuttle:eta:historical:${routeId}:${fromStopId}:${toStopId}`; - } + // Key prefixes for individual entity keys + private readonly stopKeyPrefix = 'shuttle:stop:'; + private readonly routeKeyPrefix = 'shuttle:route:'; + private readonly shuttleKeyPrefix = 'shuttle:shuttle:'; + private readonly orderedStopKeyPrefix = 'shuttle:orderedstop:'; + private readonly lastStopKeyPrefix = 'shuttle:laststop:'; + private readonly historicalEtaKeyPrefix = 'shuttle:eta:historical:'; + + // Key patterns for bulk operations (e.g., getting all keys, clearing data) + private readonly stopKeyPattern = 'shuttle:stop:*'; + private readonly routeKeyPattern = 'shuttle:route:*'; + private readonly shuttleKeyPattern = 'shuttle:shuttle:*'; + private readonly orderedStopKeyPattern = 'shuttle:orderedstop:*'; + private readonly lastStopKeyPattern = 'shuttle:laststop:*'; /** * Represents a set storing the shuttles that are currently at a stop. */ - private readonly shuttleIsAtStopKey = "shuttle:atstop"; + private readonly shuttleIsAtStopKey = 'shuttle:atstop'; + + // Helper methods for Redis key generation + private readonly createStopKey = (stopId: string) => `${this.stopKeyPrefix}${stopId}`; + private readonly createRouteKey = (routeId: string) => `${this.routeKeyPrefix}${routeId}`; + private readonly createShuttleKey = (shuttleId: string) => `${this.shuttleKeyPrefix}${shuttleId}`; + private readonly createOrderedStopKey = (routeId: string, stopId: string) => `${this.orderedStopKeyPrefix}${routeId}:${stopId}`; + private readonly createShuttleLastStopKey = (shuttleId: string) => `${this.lastStopKeyPrefix}${shuttleId}`; + private readonly createHistoricalEtaTimeSeriesKey = (routeId: string, fromStopId: string, toStopId: string) => { + return `${this.historicalEtaKeyPrefix}${routeId}:${fromStopId}:${toStopId}`; + }; // Helper methods for converting entities to Redis hashes private createRedisHashFromStop = (stop: IStop): Record => ({ @@ -227,7 +242,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt // Getter methods public async getStops(): Promise { - const keys = await this.redisClient.keys('shuttle:stop:*'); + const keys = await this.redisClient.keys(this.stopKeyPattern); const stops: IStop[] = []; for (const key of keys) { @@ -252,7 +267,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } public async getRoutes(): Promise { - const keys = await this.redisClient.keys('shuttle:route:*'); + const keys = await this.redisClient.keys(this.routeKeyPattern); const routes: IRoute[] = []; for (const key of keys) { @@ -277,7 +292,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } public async getShuttles(): Promise { - const keys = await this.redisClient.keys('shuttle:shuttle:*'); + const keys = await this.redisClient.keys(this.shuttleKeyPattern); const shuttles: IShuttle[] = []; for (const key of keys) { @@ -318,7 +333,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } public async getOrderedStopsByStopId(stopId: string): Promise { - const keys = await this.redisClient.keys('shuttle:orderedstop:*'); + const keys = await this.redisClient.keys(this.orderedStopKeyPattern); const orderedStops: IOrderedStop[] = []; for (const key of keys) { @@ -332,7 +347,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } public async getOrderedStopsByRouteId(routeId: string): Promise { - const keys = await this.redisClient.keys(`shuttle:orderedstop:${routeId}:*`); + const keys = await this.redisClient.keys(`${this.orderedStopKeyPrefix}${routeId}:*`); const orderedStops: IOrderedStop[] = []; for (const key of keys) { @@ -578,39 +593,36 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } // Clear methods - public async clearShuttleData(): Promise { - const keys = await this.redisClient.keys('shuttle:shuttle:*'); + private async clearRedisKeys(pattern: string): Promise { + const keys = await this.redisClient.keys(pattern); if (keys.length > 0) { await this.redisClient.del(keys); } + } + + public async clearShuttleData(): Promise { + await this.clearRedisKeys(this.shuttleKeyPattern); await this.clearShuttleLastStopData(); + await this.clearShuttleIsAtStopData(); } public async clearOrderedStopData(): Promise { - const keys = await this.redisClient.keys('shuttle:orderedstop:*'); - if (keys.length > 0) { - await this.redisClient.del(keys); - } + await this.clearRedisKeys(this.orderedStopKeyPattern); } public async clearRouteData(): Promise { - const keys = await this.redisClient.keys('shuttle:route:*'); - if (keys.length > 0) { - await this.redisClient.del(keys); - } + await this.clearRedisKeys(this.routeKeyPattern); } public async clearStopData(): Promise { - const keys = await this.redisClient.keys('shuttle:stop:*'); - if (keys.length > 0) { - await this.redisClient.del(keys); - } + await this.clearRedisKeys(this.stopKeyPattern); } private async clearShuttleLastStopData(): Promise { - const keys = await this.redisClient.keys('shuttle:laststop:*'); - if (keys.length > 0) { - await this.redisClient.del(keys); - } + await this.clearRedisKeys(this.lastStopKeyPattern); + } + + private async clearShuttleIsAtStopData(): Promise { + await this.clearRedisKeys(this.shuttleIsAtStopKey); } }