| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- // eslint-disable-next-line import-x/no-unresolved -- bun built-in module
- import { Database } from 'bun:sqlite';
- import os from 'os';
- import path from 'path';
- import { mkdirSync } from 'fs';
- import picocolors from 'picocolors';
- import { fastStringArrayJoin } from './misc';
- const identity = (x: any) => x;
- // eslint-disable-next-line sukka-ts/no-const-enum -- bun is smart, right?
- const enum CacheStatus {
- Hit = 'hit',
- Stale = 'stale',
- Miss = 'miss'
- }
- export interface CacheOptions<S = string> {
- /** Path to sqlite file dir */
- cachePath?: string,
- /** Time before deletion */
- tbd?: number,
- /** Cache table name */
- tableName?: string,
- type?: S extends string ? 'string' : 'buffer'
- }
- interface CacheApplyRawOption {
- ttl?: number | null,
- temporaryBypass?: boolean
- }
- interface CacheApplyNonRawOption<T, S> extends CacheApplyRawOption {
- serializer: (value: T) => S,
- deserializer: (cached: S) => T
- }
- type CacheApplyOption<T, S> = T extends S ? CacheApplyRawOption : CacheApplyNonRawOption<T, S>;
- const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
- const ONE_HOUR = 60 * 60 * 1000;
- const ONE_DAY = 24 * ONE_HOUR;
- // Add some randomness to the cache ttl to avoid thundering herd
- export const TTL = {
- humanReadable(ttl: number) {
- if (ttl >= ONE_DAY) {
- return `${Math.round(ttl / 24 / 60 / 60 / 1000)}d`;
- }
- if (ttl >= 60 * 60 * 1000) {
- return `${Math.round(ttl / 60 / 60 / 1000)}h`;
- }
- return `${Math.round(ttl / 1000)}s`;
- },
- THREE_HOURS: () => randomInt(1, 3) * ONE_HOUR,
- TWLVE_HOURS: () => randomInt(8, 12) * ONE_HOUR,
- ONE_DAY: () => randomInt(23, 25) * ONE_HOUR,
- THREE_DAYS: () => randomInt(1, 3) * ONE_DAY,
- ONE_WEEK: () => randomInt(4, 7) * ONE_DAY,
- TEN_DAYS: () => randomInt(7, 10) * ONE_DAY,
- TWO_WEEKS: () => randomInt(10, 14) * ONE_DAY
- };
- export class Cache<S = string> {
- db: Database;
- /** Time before deletion */
- tbd = 60 * 1000;
- /** SQLite file path */
- cachePath: string;
- /** Table name */
- tableName: string;
- type: S extends string ? 'string' : 'buffer';
- constructor({
- cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'),
- tbd,
- tableName = 'cache',
- type
- }: CacheOptions<S> = {}) {
- const start = Bun.nanoseconds();
- this.cachePath = cachePath;
- mkdirSync(this.cachePath, { recursive: true });
- if (tbd != null) this.tbd = tbd;
- this.tableName = tableName;
- if (type) {
- this.type = type;
- } else {
- // @ts-expect-error -- fallback type
- this.type = 'string';
- }
- const db = new Database(path.join(this.cachePath, 'cache.db'));
- db.exec('PRAGMA journal_mode = WAL;');
- db.exec('PRAGMA synchronous = normal;');
- db.exec('PRAGMA temp_store = memory;');
- db.exec('PRAGMA optimize;');
- db.prepare(`CREATE TABLE IF NOT EXISTS ${this.tableName} (key TEXT PRIMARY KEY, value ${this.type === 'string' ? 'TEXT' : 'BLOB'}, ttl REAL NOT NULL);`).run();
- db.prepare(`CREATE INDEX IF NOT EXISTS cache_ttl ON ${this.tableName} (ttl);`).run();
- const date = new Date();
- // perform purge on startup
- // ttl + tbd < now => ttl < now - tbd
- const now = date.getTime() - this.tbd;
- db.prepare(`DELETE FROM ${this.tableName} WHERE ttl < ?`).run(now);
- this.db = db;
- const dateString = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
- const lastVaccum = this.get('__LAST_VACUUM');
- if (lastVaccum === undefined || (lastVaccum !== dateString && date.getUTCDay() === 6)) {
- console.log(picocolors.magenta('[cache] vacuuming'));
- this.set('__LAST_VACUUM', dateString, 10 * 365 * 60 * 60 * 24 * 1000);
- this.db.exec('VACUUM;');
- }
- const end = Bun.nanoseconds();
- console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} cache initialized from ${this.cachePath}`);
- }
- set(key: string, value: string, ttl = 60 * 1000): void {
- const insert = this.db.prepare(
- `INSERT INTO ${this.tableName} (key, value, ttl) VALUES ($key, $value, $valid) ON CONFLICT(key) DO UPDATE SET value = $value, ttl = $valid`
- );
- insert.run({
- $key: key,
- $value: value,
- $valid: Date.now() + ttl
- });
- }
- get(key: string, defaultValue?: S): S | undefined {
- const rv = this.db.prepare<{ value: S }, string>(
- `SELECT value FROM ${this.tableName} WHERE key = ? LIMIT 1`
- ).get(key);
- if (!rv) return defaultValue;
- return rv.value;
- }
- has(key: string): CacheStatus {
- const now = Date.now();
- const rv = this.db.prepare<{ ttl: number }, string>(`SELECT ttl FROM ${this.tableName} WHERE key = ?`).get(key);
- return !rv ? CacheStatus.Miss : (rv.ttl > now ? CacheStatus.Hit : CacheStatus.Stale);
- }
- del(key: string): void {
- this.db.prepare(`DELETE FROM ${this.tableName} WHERE key = ?`).run(key);
- }
- async apply<T>(
- key: string,
- fn: () => Promise<T>,
- opt: CacheApplyOption<T, S>
- ): Promise<T> {
- const { ttl, temporaryBypass } = opt;
- if (temporaryBypass) {
- return fn();
- }
- if (ttl == null) {
- this.del(key);
- return fn();
- }
- const cached = this.get(key);
- let value: T;
- if (cached == null) {
- console.log(picocolors.yellow('[cache] miss'), picocolors.gray(key), picocolors.gray(`ttl: ${TTL.humanReadable(ttl)}`));
- const serializer = 'serializer' in opt ? opt.serializer : identity;
- const promise = fn();
- const peeked = Bun.peek(promise);
- if (peeked === promise) {
- return promise.then((value) => {
- this.set(key, serializer(value), ttl);
- return value;
- });
- }
- value = peeked as T;
- this.set(key, serializer(value), ttl);
- } else {
- console.log(picocolors.green('[cache] hit'), picocolors.gray(key));
- const deserializer = 'deserializer' in opt ? opt.deserializer : identity;
- value = deserializer(cached);
- }
- return value;
- }
- destroy() {
- this.db.close();
- }
- }
- export const fsFetchCache = new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') });
- // process.on('exit', () => {
- // fsFetchCache.destroy();
- // });
- // export const fsCache = traceSync('initializing filesystem cache', () => new Cache<Uint8Array>({ cachePath: path.resolve(import.meta.dir, '../../.cache'), type: 'buffer' }));
- const separator = '\u0000';
- // const textEncoder = new TextEncoder();
- // const textDecoder = new TextDecoder();
- // export const serializeString = (str: string) => textEncoder.encode(str);
- // export const deserializeString = (str: string) => textDecoder.decode(new Uint8Array(str.split(separator).map(Number)));
- export const serializeSet = (set: Set<string>) => fastStringArrayJoin(Array.from(set), separator);
- export const deserializeSet = (str: string) => new Set(str.split(separator));
- export const serializeArray = (arr: string[]) => fastStringArrayJoin(arr, separator);
- export const deserializeArray = (str: string) => str.split(separator);
|