mastercache
Version:
Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers
1 lines • 2.18 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/drivers/file/cleaner-worker.ts"],"sourcesContent":["// @ts-check\n\nimport { join } from 'node:path';\nimport { workerData } from 'node:worker_threads';\nimport { readdir, unlink, readFile } from 'node:fs/promises';\n\nconst directory = workerData.directory;\nconst pruneIntervalInMs = workerData.pruneInterval;\n\n/**\n * Read the file content and delete it if it's expired\n *\n * @param {string} filePath\n */\nasync function deleteFileIfExpired(filePath: string) {\n const content = await readFile(filePath, 'utf-8');\n const [, expiresAt] = JSON.parse(content);\n\n const expiry = new Date(expiresAt).getTime();\n if (+expiry === -1) return;\n\n if (expiry < Date.now()) {\n await unlink(filePath);\n }\n}\n\n/**\n * Get recursive list of files in the cache directory and delete expired files\n */\nasync function prune() {\n const dirEntries = await readdir(directory, { recursive: true, withFileTypes: true });\n\n for (const dirEntry of dirEntries) {\n if (dirEntry.isDirectory()) continue;\n\n const filePath = join(dirEntry.path, dirEntry.name);\n await deleteFileIfExpired(filePath).catch((error) => {\n console.error('[mastercache] file cleaner worker error', error);\n });\n }\n}\n\nsetInterval(async () => {\n try {\n await prune();\n } catch (error) {\n console.error('[mastercache] file cleaner worker error', error);\n }\n}, pruneIntervalInMs);\n"],"mappings":";AAEA,uBAAqB;AACrB,iCAA2B;AAC3B,sBAA0C;AAE1C,IAAM,YAAY,sCAAW;AAC7B,IAAM,oBAAoB,sCAAW;AAOrC,eAAe,oBAAoB,UAAkB;AACnD,QAAM,UAAU,UAAM,0BAAS,UAAU,OAAO;AAChD,QAAM,CAAC,EAAE,SAAS,IAAI,KAAK,MAAM,OAAO;AAExC,QAAM,SAAS,IAAI,KAAK,SAAS,EAAE,QAAQ;AAC3C,MAAI,CAAC,WAAW,GAAI;AAEpB,MAAI,SAAS,KAAK,IAAI,GAAG;AACvB,cAAM,wBAAO,QAAQ;AAAA,EACvB;AACF;AAKA,eAAe,QAAQ;AACrB,QAAM,aAAa,UAAM,yBAAQ,WAAW,EAAE,WAAW,MAAM,eAAe,KAAK,CAAC;AAEpF,aAAW,YAAY,YAAY;AACjC,QAAI,SAAS,YAAY,EAAG;AAE5B,UAAM,eAAW,uBAAK,SAAS,MAAM,SAAS,IAAI;AAClD,UAAM,oBAAoB,QAAQ,EAAE,MAAM,CAAC,UAAU;AACnD,cAAQ,MAAM,2CAA2C,KAAK;AAAA,IAChE,CAAC;AAAA,EACH;AACF;AAEA,YAAY,YAAY;AACtB,MAAI;AACF,UAAM,MAAM;AAAA,EACd,SAAS,OAAO;AACd,YAAQ,MAAM,2CAA2C,KAAK;AAAA,EAChE;AACF,GAAG,iBAAiB;","names":[]}