Skip to content

Commit

Permalink
Redis client update (#23)
Browse files Browse the repository at this point in the history
* fix: share redis client instance

* rename redisClient

* bump version to 0.7.9
  • Loading branch information
david-abell authored Apr 10, 2024
1 parent fd92b58 commit 3cf8d47
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"consola",
"Elio",
"headsign",
"hmset",
"ILIKE",
"luxon",
"ontime",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "irish-buses",
"version": "0.7.8",
"version": "0.7.9",
"type": "module",
"scripts": {
"dev": "next dev",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ export function createRedisInstance() {
throw new Error(`[Redis] Could not create a Redis instance`);
}
}

export const redisClient = createRedisInstance();
32 changes: 14 additions & 18 deletions src/pages/api/gtfs/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GTFSResponse, TripUpdate } from "@/types/realtime";
import withErrorHandler from "@/lib/withErrorHandler";
import { ApiError } from "next/dist/server/api-utils";
const API_KEY = process.env.NTA_REALTIME_API_KEY;
import { createRedisInstance } from "@/lib/redis/createRedisInstance";
import { redisClient as redis } from "@/lib/redis/redisClient";
import camelcaseKeys from "camelcase-keys";

import { StatusCodes, ReasonPhrases } from "http-status-codes";
Expand Down Expand Up @@ -38,30 +38,23 @@ const handler: ApiHandler<RealtimeTripUpdateResponse> = async (

console.log("realtime called:", new Date().toLocaleString());

const redis = createRedisInstance();

const tripUpdateKey = "tripUpdates";
const addedTripsKey = "addTrips";

// try fetch cached data
const cachedAdded = await redis.exists(addedTripsKey);
const cachedUpdates = await redis.exists(tripUpdateKey);

if (cachedAdded && cachedUpdates) {
if (!tripIds) {
console.error("No tripIds received");
res.status(StatusCodes.BAD_REQUEST).end();
return;
}
let idArray: string[] = [];

let idArray: string[] = [];
if (tripIds?.includes(",")) {
idArray = tripIds.split(",").map((tripId) => decodeURI(tripId));
} else {
idArray = !!tripIds ? [decodeURI(tripIds)] : [];
}

if (tripIds.includes(",")) {
idArray = tripIds.split(",").map((tripId) => decodeURI(tripId));
} else {
idArray = [tripIds];
}
console.log(`redis cache hit: searching for ${tripIds.length} trip ids.`);
if (cachedAdded && cachedUpdates) {
console.log(`redis cache hit: searching for ${idArray.length} trip ids.`);

const addedTripRecords = await redis.hgetall(addedTripsKey);

Expand All @@ -87,7 +80,7 @@ const handler: ApiHandler<RealtimeTripUpdateResponse> = async (
return res.status(StatusCodes.OK).json({ addedTrips, tripUpdates });
}

console.log(`redis cache miss, setting new trip updates`);
console.log("redis cache miss, setting new trip updates");

const response = await fetch(API_URL, {
method: "GET",
Expand All @@ -108,6 +101,9 @@ const handler: ApiHandler<RealtimeTripUpdateResponse> = async (

const data: GTFSResponse = camelcaseKeys(json, { deep: true });

console.log("Downloaded %s trip updates", data?.entity?.length);
const requestedTripSet = tripIds ? new Set(idArray) : new Set();

const tripUpdates = new Map<string, string>();
const addedTripsMap = new Map<string, string>();
const requestedTripUpdates: [string, TripUpdate][] = [];
Expand All @@ -129,7 +125,7 @@ const handler: ApiHandler<RealtimeTripUpdateResponse> = async (
}
tripUpdates.set(tripUpdate.trip.tripId, JSON.stringify(tripUpdate));

if (tripIds?.includes(tripUpdate.trip.tripId)) {
if (requestedTripSet.has(tripUpdate.trip.tripId)) {
requestedTripUpdates.push([tripUpdate.trip.tripId, tripUpdate]);
}

Expand Down

0 comments on commit 3cf8d47

Please sign in to comment.