final product
This commit is contained in:
@@ -58,7 +58,7 @@ async function loadGtfsStaticInMemory() {
|
||||
const gtfs = {};
|
||||
|
||||
for (const entry of directory.files) {
|
||||
if (!entry.path.endsWith(".txt")) continue; // only GTFS .txt files
|
||||
if (!entry.path.endsWith(".txt")) continue;
|
||||
|
||||
const fileBuffer = await entry.buffer();
|
||||
const text = fileBuffer.toString("utf8");
|
||||
@@ -315,10 +315,6 @@ module.exports = {
|
||||
getScheduleByRoute,
|
||||
getScheduleByStationId,
|
||||
getShapeByRoute,
|
||||
|
||||
updateGtfsRt,
|
||||
loadGtfsStaticInMemory,
|
||||
gtfs_rt_v,
|
||||
gtfs_rt_t,
|
||||
gtfs_rt_a,
|
||||
}
|
||||
|
||||
99
tools/gtfsRedis.js
Normal file
99
tools/gtfsRedis.js
Normal file
@@ -0,0 +1,99 @@
|
||||
const gtfs = require("./gtfsData");
|
||||
const redis = require("./redisDAL");
|
||||
|
||||
const CACHE_PREFIX = "gtfs_cache";
|
||||
const RT_TTL = 3;
|
||||
const STATIC_TTL = 24 * 3600;
|
||||
|
||||
async function getCached(keySuffix, fetchFn, ttl) {
|
||||
await redis.connect();
|
||||
|
||||
const cached = await redis.get(CACHE_PREFIX, { key: keySuffix });
|
||||
if (cached !== null) return cached;
|
||||
|
||||
const data = await fetchFn();
|
||||
await redis.set(CACHE_PREFIX, { key: keySuffix }, data, ttl);
|
||||
return data;
|
||||
}
|
||||
|
||||
async function getTrains() {
|
||||
return getCached("trains", gtfs.getTrains, RT_TTL);
|
||||
}
|
||||
|
||||
async function getBuses() {
|
||||
return getCached("buses", gtfs.getBuses, RT_TTL);
|
||||
}
|
||||
|
||||
async function getTrainsByRoute(route) {
|
||||
return getCached(`trains_route_${route}`, () => gtfs.getTrainsByRoute(route), RT_TTL);
|
||||
}
|
||||
|
||||
async function getBusesByRoute(route) {
|
||||
return getCached(`buses_route_${route}`, () => gtfs.getBusesByRoute(route), RT_TTL);
|
||||
}
|
||||
|
||||
async function getAlerts() {
|
||||
return getCached("alerts", gtfs.getAlerts, RT_TTL);
|
||||
}
|
||||
|
||||
async function getAlertsByRoute(route) {
|
||||
return getCached(`alerts_route_${route}`, () => gtfs.getAlertsByRoute(route), RT_TTL);
|
||||
}
|
||||
|
||||
async function getScheduleByRoute(route) {
|
||||
return getCached(`schedule_route_${route}`, () => gtfs.getScheduleByRoute(route), RT_TTL);
|
||||
}
|
||||
|
||||
async function getScheduleByStationId(stopId) {
|
||||
return getCached(`schedule_stop_${stopId}`, () => gtfs.getScheduleByStationId(stopId), RT_TTL);
|
||||
}
|
||||
|
||||
async function getRoutes() {
|
||||
return getCached("routes", gtfs.getRoutes, STATIC_TTL);
|
||||
}
|
||||
|
||||
async function getStops() {
|
||||
return getCached("stops", gtfs.getStops, STATIC_TTL);
|
||||
}
|
||||
|
||||
async function getStopsByRoute(route) {
|
||||
return getCached(`stops_route_${route}`, () => gtfs.getStopsByRoute(route), STATIC_TTL);
|
||||
}
|
||||
|
||||
async function getShapeByRoute(route) {
|
||||
return getCached(`shape_route_${route}`, () => gtfs.getShapeByRoute(route), STATIC_TTL);
|
||||
}
|
||||
|
||||
async function updateGtfsRt() {
|
||||
const data = await gtfs.updateGtfsRt();
|
||||
await redis.set(CACHE_PREFIX, { key: "trains" }, await gtfs.getTrains(), RT_TTL);
|
||||
await redis.set(CACHE_PREFIX, { key: "buses" }, await gtfs.getBuses(), RT_TTL);
|
||||
await redis.set(CACHE_PREFIX, { key: "alerts" }, await gtfs.getAlerts(), RT_TTL);
|
||||
return data;
|
||||
}
|
||||
|
||||
async function loadGtfsStaticInMemory() {
|
||||
const data = await gtfs.loadGtfsStaticInMemory();
|
||||
await redis.set(CACHE_PREFIX, { key: "routes" }, data.routes || [], STATIC_TTL);
|
||||
await redis.set(CACHE_PREFIX, { key: "stops" }, data.stops || [], STATIC_TTL);
|
||||
return data;
|
||||
}
|
||||
|
||||
const { gtfs_rt_v, gtfs_rt_t, gtfs_rt_a } = gtfs;
|
||||
|
||||
module.exports = {
|
||||
getStops,
|
||||
getStopsByRoute,
|
||||
getRoutes,
|
||||
getTrains,
|
||||
getBuses,
|
||||
getTrainsByRoute,
|
||||
getBusesByRoute,
|
||||
getAlerts,
|
||||
getAlertsByRoute,
|
||||
getScheduleByRoute,
|
||||
getScheduleByStationId,
|
||||
getShapeByRoute,
|
||||
updateGtfsRt,
|
||||
loadGtfsStaticInMemory,
|
||||
};
|
||||
45
tools/redisDAL.js
Normal file
45
tools/redisDAL.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const { createClient } = require("redis");
|
||||
|
||||
class RedisDAL {
|
||||
constructor() {
|
||||
this.client = createClient({ url:"redis://my-redis:6379" });
|
||||
|
||||
this.client.on("error", (err) => {
|
||||
console.error("Redis Client Error", err);
|
||||
});
|
||||
}
|
||||
|
||||
async connect() {
|
||||
if (!this.client.isOpen) {
|
||||
await this.client.connect();
|
||||
console.log("Connected to Redis");
|
||||
}
|
||||
}
|
||||
|
||||
generateKey(prefix, params) {
|
||||
const paramString = Object.entries(params)
|
||||
.sort()
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join("&");
|
||||
return `${prefix}:${paramString}`;
|
||||
}
|
||||
|
||||
async get(prefix, params) {
|
||||
const key = this.generateKey(prefix, params);
|
||||
const cached = await this.client.get(key);
|
||||
if (cached) return JSON.parse(cached);
|
||||
return null;
|
||||
}
|
||||
|
||||
async set(prefix, params, data, ttl = 3600) {
|
||||
const key = this.generateKey(prefix, params);
|
||||
await this.client.set(key, JSON.stringify(data), { EX: ttl });
|
||||
}
|
||||
|
||||
async del(prefix, params) {
|
||||
const key = this.generateKey(prefix, params);
|
||||
await this.client.del(key);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new RedisDAL();
|
||||
Reference in New Issue
Block a user