Skip to content

Commit

Permalink
feat: implement caching in active api
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarMulder committed Jul 16, 2022
1 parent 42f5e1b commit 934d0cc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"@prisma/client": "^4.0.0",
"cors": "^2.8.5",
"next": "12.2.0",
"node-cache": "^5.1.2",
"react": "18.2.0"
},
"devDependencies": {
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
"@semantic-release-plus/docker": "^3.1.2",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.5",
"@types/cors": "^2.8.12",
"@types/node": "18.0.0",
Expand Down
2 changes: 1 addition & 1 deletion public/js/interactive_maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5380,7 +5380,7 @@ var $author$project$Main$Window = F2(
return {aM: height, a5: width};
});
var $author$project$Endpoint$Endpoint = $elm$core$Basics$identity;
var $author$project$Endpoint$activeSessions = 'http://localhost:3000/api/active';
var $author$project$Endpoint$activeSessions = 'api/active';
var $elm$core$Platform$Cmd$batch = _Platform_batch;
var $author$project$Clustermap$MapSettings = F4(
function (height, width, activeIconSize, emptyIconSize) {
Expand Down
61 changes: 34 additions & 27 deletions src/pages/api/active.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "../../server/db/client";
import Cors from "cors";
import NodeCache from "node-cache";

const locationCache = new NodeCache();

const cors = Cors({
methods: ['POST', 'GET', 'HEAD'],
})

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(
req: NextApiRequest,
res: NextApiResponse,
fn: Function
) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result)
}

return resolve(result)
})
methods: ['POST', 'GET', 'HEAD'],
})

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(
req: NextApiRequest,
res: NextApiResponse,
fn: Function
) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result)
}

return resolve(result)
})
}
})
}

const locations = async (req: NextApiRequest, res: NextApiResponse) => {
await runMiddleware(req, res, cors);
await runMiddleware(req, res, cors);

const locations = await prisma.location.findMany({
where: {
let locations = locationCache.get("locations");
if (locations == undefined) {
const db_locations = await prisma.location.findMany({
where: {
end_at: null,
},
select: {
},
select: {
login: true,
hostname: true,
}
});

}
});
locationCache.set("locations", db_locations, 5);
locations = db_locations;
}
res.status(200).json(locations);
};

Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,11 @@ cliui@^7.0.2:
strip-ansi "^6.0.0"
wrap-ansi "^7.0.0"

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==

clone@^1.0.2:
version "1.0.4"
resolved "https://registry.yarnpkg.com/clone/-/clone-1.0.4.tgz#da309cc263df15994c688ca902179ca3c7cd7c7e"
Expand Down Expand Up @@ -3159,6 +3164,13 @@ [email protected]:
"@next/swc-win32-ia32-msvc" "12.2.0"
"@next/swc-win32-x64-msvc" "12.2.0"

node-cache@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==
dependencies:
clone "2.x"

node-emoji@^1.11.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.11.0.tgz#69a0150e6946e2f115e9d7ea4df7971e2628301c"
Expand Down

0 comments on commit 934d0cc

Please sign in to comment.