Skip to content

Commit

Permalink
set size
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Dec 1, 2024
1 parent ca909ce commit 87b9425
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
29 changes: 9 additions & 20 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"fastify": "^4.26.2",
"graphql": "^16.0.0",
"graphql-request": "^6.1.0",
"node-cache": "^5.1.2",
"lru-cache": "^11.0.2",
"node-fetch": "^2.6.7",
"object-hash": "^3.0.0",
"postgres": "^3.2.4",
Expand Down
25 changes: 8 additions & 17 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import NodeCache from "node-cache";
import { LRUCache } from "lru-cache";
import hash from "object-hash";

const MAX_KEYS = 10000;
const CLEANUP_THRESHOLD = 0.9;
const MAX_SIZE_BYTES = 100 * 1024 * 1024;

export const cache = new NodeCache({
stdTTL: 600,
checkperiod: 120,
maxKeys: Number.MAX_SAFE_INTEGER,
useClones: false,
deleteOnExpire: true,
});

cache.on("set", () => {
const keys = cache.keys();
if (keys.length > MAX_KEYS * CLEANUP_THRESHOLD) {
const keysToRemove = keys.slice(0, Math.floor(MAX_KEYS * 0.2));
keysToRemove.forEach((key) => cache.del(key));
}
export const cache = new LRUCache({
maxSize: MAX_SIZE_BYTES,
sizeCalculation: (value: any) => {
return Buffer.byteLength(JSON.stringify(value), "utf8");
},
ttl: 1000 * 600,
});

interface APIEvent {
Expand Down

0 comments on commit 87b9425

Please sign in to comment.