Skip to content

Commit

Permalink
Cache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Eghizio committed Sep 16, 2021
1 parent fdb5d2b commit 67b062e
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 60 deletions.
8 changes: 2 additions & 6 deletions src/api/alliances.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// "/api/alliances"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { Alliances } from "../types/api";


export const alliancesRouter = express.Router();
const cache = new CacheService();

alliancesRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
alliancesRouter.get("/", cache(0), (req, res) => {

fetch(OGAME_API_ENDPOINTS.alliances(TEMP_SERVER_ID))
.then(response => response.text())
Expand All @@ -40,7 +37,6 @@ alliancesRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
16 changes: 4 additions & 12 deletions src/api/highscore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// "/api/highscore"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";


export const highscoreRouter = express.Router();
const cache = new CacheService();


highscoreRouter.get("/", (req, res) => {
const guide = {
Expand All @@ -29,10 +29,7 @@ highscoreRouter.get("/", (req, res) => {
});


highscoreRouter.get("/players", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
highscoreRouter.get("/players", cache(0), (req, res) => {

const { query: q } = req;
const type = (q.type && (Number(q.type) >= 0 && Number(q.type) <= 7)) ? q.type : "0";
Expand All @@ -54,15 +51,11 @@ highscoreRouter.get("/players", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});

highscoreRouter.get("/alliances", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
highscoreRouter.get("/alliances", cache(0), (req, res) => {

const { query: q } = req;
const type = (q.type && (Number(q.type) >= 0 && Number(q.type) <= 7)) ? q.type : "0";
Expand All @@ -82,7 +75,6 @@ highscoreRouter.get("/alliances", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
10 changes: 3 additions & 7 deletions src/api/localization.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// "/api/localization"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { Localization } from "../types/api";


export const localizationRouter = express.Router();
const cache = new CacheService();

localizationRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));

localizationRouter.get("/", cache(0), (req, res) => {

const URL = OGAME_API_ENDPOINTS.localization(TEMP_SERVER_ID);

Expand Down Expand Up @@ -53,7 +50,6 @@ localizationRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
9 changes: 2 additions & 7 deletions src/api/playerData.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// "/api/playerData"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { PlayerData } from "../types/api";


export const playerDataRouter = express.Router();
const cache = new CacheService();

// TODO highscore and alliance (test for id=[1, 100013, 101421])
playerDataRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
playerDataRouter.get("/", cache(0), (req, res) => {

const { query: q } = req;
const id = q.id ? q.id : "1";
Expand Down Expand Up @@ -61,7 +57,6 @@ playerDataRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
9 changes: 2 additions & 7 deletions src/api/players.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// "/api/players"
import express from "express";
import fetch from "node-fetch";
import { CacheService } from "../services/CacheService";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { Players } from "../types/api";


export const playersRouter = express.Router();
const cache = new CacheService();

playersRouter.get("/", (req, res) => {
playersRouter.get("/", cache(0), (req, res) => {
// console.log(`Calling "${req.originalUrl}"...`); // need to npm morgan for logs

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));

const URL = OGAME_API_ENDPOINTS.players(TEMP_SERVER_ID);

fetch(URL)
Expand All @@ -30,7 +26,6 @@ playersRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
9 changes: 2 additions & 7 deletions src/api/serverData.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// "/api/serverData"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";


export const serverDataRouter = express.Router();
const cache = new CacheService();

serverDataRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
serverDataRouter.get("/", cache(0), (req, res) => {

const URL = OGAME_API_ENDPOINTS.serverData(TEMP_SERVER_ID);

Expand All @@ -32,7 +28,6 @@ serverDataRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
10 changes: 3 additions & 7 deletions src/api/universe.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// "/api/universe"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { Universe } from "../types/api";


export const universeRouter = express.Router();
const cache = new CacheService();

universeRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
// split into galaxies? filtering by galaxies, systems, positions?
universeRouter.get("/", cache(0), (req, res) => {

const URL = OGAME_API_ENDPOINTS.universe(TEMP_SERVER_ID);

Expand All @@ -36,7 +33,6 @@ universeRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
9 changes: 2 additions & 7 deletions src/api/universes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// "/api/universes"
import express from "express";
import fetch from "node-fetch";
import { cache } from "../middlewares/cache";
import { XMLParserService } from "../services/XMLParserService";
import { CacheService } from "../services/CacheService";
import { OGAME_API_ENDPOINTS, TEMP_SERVER_ID } from "../constants/endpoints";
import { Universes } from "../types/api";


export const universesRouter = express.Router();
const cache = new CacheService();

universesRouter.get("/", (req, res) => {

if(cache.has(req.originalUrl))
return res.json(cache.get(req.originalUrl));
universesRouter.get("/", cache(0), (req, res) => {

const URL = OGAME_API_ENDPOINTS.universes(TEMP_SERVER_ID);

Expand All @@ -29,7 +25,6 @@ universesRouter.get("/", (req, res) => {

return orderedJSON;
})
.then(data => cache.set(req.originalUrl, data))
.then(formatedJSON => res.json(formatedJSON))
.catch(err => res.send(err));
});
23 changes: 23 additions & 0 deletions src/middlewares/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RequestHandler } from "express";
import { CacheService } from "../services/CacheService";

export const cache = (durationMS: number): RequestHandler => {
const cache = new CacheService();

return (req, res, next) => {
const key = req.originalUrl || req.url;
const cachedData = cache.get(key);

res.type("application/json"); // fixes type coz hack

if(cachedData) return res.send(cachedData);

const sendResponse = res.send.bind(res); // ts hack to not add sendResponse (res.sendResponse)
res.send = (data) => {
cache.set(key, data); //durationMS * 1000
return sendResponse(data);
};

next();
};
};
3 changes: 3 additions & 0 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Router } from "express";

export const api = Router();
1 change: 1 addition & 0 deletions src/services/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export class CacheService{
cache: Map<string, any>
constructor(){
this.cache = new Map();
console.log("Created cache") //debug test
}

set(key: string, value: any){
Expand Down

0 comments on commit 67b062e

Please sign in to comment.