Refactor all Redis keys to use variables, and add clearing of shuttle isAtStop set on shuttle clear

This commit is contained in:
2025-11-23 20:29:06 -08:00
parent 8d2dd60cfb
commit 19cdbec42b

View File

@@ -82,20 +82,35 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
return super.emit(event, ...args); return super.emit(event, ...args);
} }
// Helper methods for Redis key generation // Key prefixes for individual entity keys
private createStopKey = (stopId: string) => `shuttle:stop:${stopId}`; private readonly stopKeyPrefix = 'shuttle:stop:';
private createRouteKey = (routeId: string) => `shuttle:route:${routeId}`; private readonly routeKeyPrefix = 'shuttle:route:';
private createShuttleKey = (shuttleId: string) => `shuttle:shuttle:${shuttleId}`; private readonly shuttleKeyPrefix = 'shuttle:shuttle:';
private createOrderedStopKey = (routeId: string, stopId: string) => `shuttle:orderedstop:${routeId}:${stopId}`; private readonly orderedStopKeyPrefix = 'shuttle:orderedstop:';
private createShuttleLastStopKey = (shuttleId: string) => `shuttle:laststop:${shuttleId}`; private readonly lastStopKeyPrefix = 'shuttle:laststop:';
private createHistoricalEtaTimeSeriesKey = (routeId: string, fromStopId: string, toStopId: string) => { private readonly historicalEtaKeyPrefix = 'shuttle:eta:historical:';
return `shuttle:eta:historical:${routeId}:${fromStopId}:${toStopId}`;
} // 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. * 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 // Helper methods for converting entities to Redis hashes
private createRedisHashFromStop = (stop: IStop): Record<string, string> => ({ private createRedisHashFromStop = (stop: IStop): Record<string, string> => ({
@@ -227,7 +242,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
// Getter methods // Getter methods
public async getStops(): Promise<IStop[]> { public async getStops(): Promise<IStop[]> {
const keys = await this.redisClient.keys('shuttle:stop:*'); const keys = await this.redisClient.keys(this.stopKeyPattern);
const stops: IStop[] = []; const stops: IStop[] = [];
for (const key of keys) { for (const key of keys) {
@@ -252,7 +267,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
} }
public async getRoutes(): Promise<IRoute[]> { public async getRoutes(): Promise<IRoute[]> {
const keys = await this.redisClient.keys('shuttle:route:*'); const keys = await this.redisClient.keys(this.routeKeyPattern);
const routes: IRoute[] = []; const routes: IRoute[] = [];
for (const key of keys) { for (const key of keys) {
@@ -277,7 +292,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
} }
public async getShuttles(): Promise<IShuttle[]> { public async getShuttles(): Promise<IShuttle[]> {
const keys = await this.redisClient.keys('shuttle:shuttle:*'); const keys = await this.redisClient.keys(this.shuttleKeyPattern);
const shuttles: IShuttle[] = []; const shuttles: IShuttle[] = [];
for (const key of keys) { for (const key of keys) {
@@ -318,7 +333,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
} }
public async getOrderedStopsByStopId(stopId: string): Promise<IOrderedStop[]> { public async getOrderedStopsByStopId(stopId: string): Promise<IOrderedStop[]> {
const keys = await this.redisClient.keys('shuttle:orderedstop:*'); const keys = await this.redisClient.keys(this.orderedStopKeyPattern);
const orderedStops: IOrderedStop[] = []; const orderedStops: IOrderedStop[] = [];
for (const key of keys) { for (const key of keys) {
@@ -332,7 +347,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
} }
public async getOrderedStopsByRouteId(routeId: string): Promise<IOrderedStop[]> { public async getOrderedStopsByRouteId(routeId: string): Promise<IOrderedStop[]> {
const keys = await this.redisClient.keys(`shuttle:orderedstop:${routeId}:*`); const keys = await this.redisClient.keys(`${this.orderedStopKeyPrefix}${routeId}:*`);
const orderedStops: IOrderedStop[] = []; const orderedStops: IOrderedStop[] = [];
for (const key of keys) { for (const key of keys) {
@@ -578,39 +593,36 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
} }
// Clear methods // Clear methods
public async clearShuttleData(): Promise<void> { private async clearRedisKeys(pattern: string): Promise<void> {
const keys = await this.redisClient.keys('shuttle:shuttle:*'); const keys = await this.redisClient.keys(pattern);
if (keys.length > 0) { if (keys.length > 0) {
await this.redisClient.del(keys); await this.redisClient.del(keys);
} }
}
public async clearShuttleData(): Promise<void> {
await this.clearRedisKeys(this.shuttleKeyPattern);
await this.clearShuttleLastStopData(); await this.clearShuttleLastStopData();
await this.clearShuttleIsAtStopData();
} }
public async clearOrderedStopData(): Promise<void> { public async clearOrderedStopData(): Promise<void> {
const keys = await this.redisClient.keys('shuttle:orderedstop:*'); await this.clearRedisKeys(this.orderedStopKeyPattern);
if (keys.length > 0) {
await this.redisClient.del(keys);
}
} }
public async clearRouteData(): Promise<void> { public async clearRouteData(): Promise<void> {
const keys = await this.redisClient.keys('shuttle:route:*'); await this.clearRedisKeys(this.routeKeyPattern);
if (keys.length > 0) {
await this.redisClient.del(keys);
}
} }
public async clearStopData(): Promise<void> { public async clearStopData(): Promise<void> {
const keys = await this.redisClient.keys('shuttle:stop:*'); await this.clearRedisKeys(this.stopKeyPattern);
if (keys.length > 0) {
await this.redisClient.del(keys);
}
} }
private async clearShuttleLastStopData(): Promise<void> { private async clearShuttleLastStopData(): Promise<void> {
const keys = await this.redisClient.keys('shuttle:laststop:*'); await this.clearRedisKeys(this.lastStopKeyPattern);
if (keys.length > 0) { }
await this.redisClient.del(keys);
} private async clearShuttleIsAtStopData(): Promise<void> {
await this.clearRedisKeys(this.shuttleIsAtStopKey);
} }
} }