Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

feat(cache): add caching to statistics #22

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import express from "express";
import { flatten } from "lodash/fp";

import packageJson from "../package.json";
import { createCache } from "./caching/caching-service";
import { corsOrigins, port } from "./config";
import { getGitHubData } from "./github/github-service";
import { matomoConfig } from "./matomo/matomo-config";
import { getMultiSiteMatomoData } from "./matomo/matomo-service";
import { getYoutubeData } from "./youtube/youtube-service";

const CACHE_TTL = 10 * 60 * 1000;

const app = express();

app.use(
Expand All @@ -25,14 +28,28 @@ app.get("/healthz", (req, res) => {
res.send("OK");
});

const statsCache = createCache(
async () =>
Promise.all([
getMultiSiteMatomoData(matomoConfig),
getYoutubeData(),
getGitHubData(),
]).then(flatten),
CACHE_TTL
);

app.get("/statistics", (req, res) => {
void Promise.all([
getMultiSiteMatomoData(matomoConfig),
getYoutubeData(),
getGitHubData(),
])
.then(flatten)
.then((data) => res.json(data));
res.json({
lastFetchTimestamp: statsCache.getLastFetchTimestamp(),
result: statsCache.read(),
});
});

app.post("/refresh", (req, res) => {
void statsCache
.refresh()
.then(() => res.status(204).send())
.catch(() => res.status(500).send("Error refreshing cache"));
});

app.listen(port, () => {
Expand Down
14 changes: 14 additions & 0 deletions src/caching/caching-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createCache } from "./caching-service";

describe("caching-utils", () => {
describe("createCache", () => {
it("reads cache with fetch data value", async () => {
const CACHE_TTL = 10 * 60 * 1000;
const cacheValue = ["foo", "bar"];

const { read, refresh } = createCache(() => cacheValue, CACHE_TTL);
await refresh();
expect(read()).toEqual(cacheValue);
});
});
});
28 changes: 28 additions & 0 deletions src/caching/caching-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type CacheObject<T> = {
read: () => T | null;
refresh: () => Promise<void>;
getLastFetchTimestamp: () => number | null;
};

type FetchData<T> = () => Promise<T> | T;

export const createCache = <T>(
fetchData: FetchData<T>,
ttl: number
): CacheObject<T> => {
let cacheData: null | T = null;
let lastFetchTimestamp = 0;
let refreshTimeout: NodeJS.Timeout | null = null;
const refresh = async () => {
refreshTimeout?.unref();
cacheData = await fetchData();
lastFetchTimestamp = Date.now();
refreshTimeout = setTimeout(() => void refresh(), ttl);
};
void refresh();
return {
getLastFetchTimestamp: () => lastFetchTimestamp,
read: () => cacheData,
refresh,
};
};