Files
olympics-calendar/scraper/cache.ts
Fabrice Lamant fef4690b0b add docker
2025-12-06 14:55:13 +01:00

33 lines
851 B
TypeScript

import Debug from "debug";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
export class Cache {
private debug = Debug("olympics-calendar:cache");
private cachePath = (key: string): string => {
return `./cache/${key}.cached`;
};
public get(key: string): string | null {
this.debug("get", key);
const path = this.cachePath(key);
if (existsSync(path)) {
return readFileSync(path, "utf-8");
}
return null;
}
public has(key: string): boolean {
this.debug(`has: key=${key}`);
const path = this.cachePath(key);
return existsSync(path);
}
public set(key: string, data: string): void {
this.debug(`set: key=${key}`);
const path = this.cachePath(key);
mkdirSync(path.split("/").slice(0, -1).join("/"), { recursive: true });
writeFileSync(path, data);
}
}