Skip to content

Commit

Permalink
Merge pull request #421 from siddharth-sahoo/dev
Browse files Browse the repository at this point in the history
Use node-cache for report-store
  • Loading branch information
siddharth-sahoo authored Oct 11, 2024
2 parents 5fbc84a + 015b50b commit 49d945c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
22 changes: 22 additions & 0 deletions services/ad-tech/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion services/ad-tech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"scripts": {
"start": "npm run build && node ./build/main.js",
"build": "tsc && cp -r src/views build/",
"ncu": "npx npm-check-updates -u"
"ncu": "npx npm-check-updates -u",
"fmt": "pre-commit run --all-files"
},
"dependencies": {
"cbor": "^9.0.2",
"cors": "^2.8.5",
"ejs": "^3.1.9",
"express": "^4.18.2",
"node-cache": "^5.1.2",
"structured-field-values": "^2.0.1"
},
"devDependencies": {
Expand Down
20 changes: 10 additions & 10 deletions services/ad-tech/src/controllers/report-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/

import NodeCache from 'node-cache';

/** Basic categories of reports. */
export enum ReportCategory {
EVENT_LEVEL_LOG,
Expand All @@ -34,25 +36,23 @@ export interface Report {
data: any;
}

/** TTL for in-memory reports: 10 minutes */
export const REPORT_TTL_SECONDS = 10 * 60;

/** Simple in-memory implementation of report storage. */
export const ReportStore = (() => {
// In-memory storage for reports.
const Reports: Report[] = [];
// Clear in-memory storage every 10 min
setInterval(
() => {
Reports.length = 0;
},
1000 * 60 * 10,
);
const Reports = new NodeCache({stdTTL: REPORT_TTL_SECONDS});

/** Add a new report to the in-memory storage. */
const addReport = (report: Report) => {
Reports.push(report);
// TODO: Consider partitioning by use-case.
Reports.set(`${report.category}||${report.timestamp}`, report);
};

/** Returns all reports from in-memory storage. */
const getAllReports = (): Report[] => {
return [...Reports];
return Reports.keys().map((key) => Reports.get(key) as Report);
};

return {
Expand Down

0 comments on commit 49d945c

Please sign in to comment.