signalk-tides
Version:
Tidal predictions for the vessel's position from various online sources.
26 lines (25 loc) • 666 B
JavaScript
import { join } from "path";
import { writeFile, readFile, mkdir } from "fs/promises";
export default class FileCache {
path;
constructor(path) {
this.path = path;
}
async get(key) {
try {
const content = await readFile(this.getKey(key), "utf-8");
return JSON.parse(content);
}
catch {
return undefined;
}
}
async set(key, value) {
// Ensure directory exists
await mkdir(this.path, { recursive: true });
return writeFile(this.getKey(key), JSON.stringify(value));
}
getKey(key) {
return join(this.path, `${key}.json`);
}
}