UNPKG

mastercache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

33 lines 1.3 kB
// src/drivers/file/cleaner-worker.ts var import_node_path = require("path"); var import_node_worker_threads = require("worker_threads"); var import_promises = require("fs/promises"); var directory = import_node_worker_threads.workerData.directory; var pruneIntervalInMs = import_node_worker_threads.workerData.pruneInterval; async function deleteFileIfExpired(filePath) { const content = await (0, import_promises.readFile)(filePath, "utf-8"); const [, expiresAt] = JSON.parse(content); const expiry = new Date(expiresAt).getTime(); if (+expiry === -1) return; if (expiry < Date.now()) { await (0, import_promises.unlink)(filePath); } } async function prune() { const dirEntries = await (0, import_promises.readdir)(directory, { recursive: true, withFileTypes: true }); for (const dirEntry of dirEntries) { if (dirEntry.isDirectory()) continue; const filePath = (0, import_node_path.join)(dirEntry.path, dirEntry.name); await deleteFileIfExpired(filePath).catch((error) => { console.error("[mastercache] file cleaner worker error", error); }); } } setInterval(async () => { try { await prune(); } catch (error) { console.error("[mastercache] file cleaner worker error", error); } }, pruneIntervalInMs); //# sourceMappingURL=cleaner-worker.cjs.map