Skip to content

Commit

Permalink
better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Jan 20, 2024
1 parent 1be618d commit 3967615
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ const aw = (cb) => {
};

// CACHE
const CACHE = apicache.middleware;
app.use(CACHE("5 minutes"));
const cache = apicache.middleware;
const onlyStatus200 = (_req, res) => res.statusCode === 200;
const cacheSuccesses = cache("5 minutes", onlyStatus200);

// ROUTES
router.get(
Expand Down Expand Up @@ -191,7 +192,8 @@ router.post(
);
router.get(
"/gallery",
aw(async (req, res, _next) => {
cacheSuccesses,
aw(async (req, res) => {
const page = parseInt(req.query.page) || undefined;
const pageSize = parseInt(req.query.pageSize) || undefined;
const ip =
Expand All @@ -208,7 +210,8 @@ router.get(
// ROUTES
router.get(
"/blog/posts/:postId/:slug",
aw(async (req, res, _next) => {
cacheSuccesses,
aw(async (req, res) => {
const id = req.params.postId;
const slug = req.params.slug;
const ip =
Expand All @@ -224,7 +227,8 @@ router.get(
);
router.get(
"/blog",
aw(async (req, res, _next) => {
cacheSuccesses,
aw(async (req, res) => {
const page = parseInt(req.query.page) || undefined;
const pageSize = parseInt(req.query.pageSize) || undefined;
const ip =
Expand All @@ -241,7 +245,8 @@ router.get(

router.get(
"/",
aw(async (req, res, _next) => {
cacheSuccesses,
aw(async (req, res) => {
const ip =
req.headers["x-forwarded-for"]?.split(",")[0] ||
req.socket.remoteAddress ||
Expand Down

0 comments on commit 3967615

Please sign in to comment.