Skip to content

Commit

Permalink
Use node-cache for report-store.
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharth-sahoo committed Oct 8, 2024
1 parent baba88a commit c43babc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 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.

1 change: 1 addition & 0 deletions services/ad-tech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"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
22 changes: 12 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,25 @@ 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);
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 c43babc

Please sign in to comment.