cache-filesystem.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // eslint-disable-next-line import/no-unresolved -- bun built-in module
  2. import { Database } from 'bun:sqlite';
  3. import os from 'os';
  4. import path from 'path';
  5. import fs from 'fs';
  6. import picocolors from 'picocolors';
  7. const identity = (x: any) => x;
  8. // eslint-disable-next-line sukka-ts/no-const-enum -- bun is smart, right?
  9. const enum CacheStatus {
  10. Hit = 'hit',
  11. Stale = 'stale',
  12. Miss = 'miss'
  13. }
  14. export interface CacheOptions {
  15. cachePath?: string,
  16. tbd?: number
  17. }
  18. interface CacheApplyNonStringOption<T> {
  19. ttl?: number | null,
  20. serializer: (value: T) => string,
  21. deserializer: (cached: string) => T,
  22. temporaryBypass?: boolean
  23. }
  24. interface CacheApplyStringOption {
  25. ttl?: number | null,
  26. temporaryBypass?: boolean
  27. }
  28. type CacheApplyOption<T> = T extends string ? CacheApplyStringOption : CacheApplyNonStringOption<T>;
  29. export class Cache {
  30. db: Database;
  31. tbd = 60 * 1000; // time before deletion
  32. cachePath: string;
  33. constructor({ cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'), tbd }: CacheOptions = {}) {
  34. this.cachePath = cachePath;
  35. fs.mkdirSync(this.cachePath, { recursive: true });
  36. if (tbd != null) this.tbd = tbd;
  37. const db = new Database(path.join(this.cachePath, 'cache.db'));
  38. db.exec('PRAGMA journal_mode = WAL');
  39. db.prepare('CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, value TEXT, ttl REAL NOT NULL);').run();
  40. db.prepare('CREATE INDEX IF NOT EXISTS cache_ttl ON cache (ttl);').run();
  41. // perform purge on startup
  42. // ttl + tbd < now => ttl < now - tbd
  43. const now = Date.now() - this.tbd;
  44. db.prepare('DELETE FROM cache WHERE ttl < ?').run(now);
  45. this.db = db;
  46. }
  47. set(key: string, value: string, ttl = 60 * 1000): void {
  48. const insert = this.db.prepare(
  49. 'INSERT INTO cache (key, value, ttl) VALUES ($key, $value, $valid) ON CONFLICT(key) DO UPDATE SET value = $value, ttl = $valid'
  50. );
  51. insert.run({
  52. $key: key,
  53. $value: value,
  54. $valid: Date.now() + ttl
  55. });
  56. }
  57. get(key: string, defaultValue?: string): string | undefined {
  58. const rv = this.db.prepare<{ value: string }, string>(
  59. 'SELECT value FROM cache WHERE key = ?'
  60. ).get(key);
  61. if (!rv) return defaultValue;
  62. return rv.value;
  63. }
  64. has(key: string): CacheStatus {
  65. const now = Date.now();
  66. const rv = this.db.prepare<{ ttl: number }, string>('SELECT ttl FROM cache WHERE key = ?').get(key);
  67. return !rv ? CacheStatus.Miss : (rv.ttl > now ? CacheStatus.Hit : CacheStatus.Stale);
  68. }
  69. del(key: string): void {
  70. this.db.prepare('DELETE FROM cache WHERE key = ?').run(key);
  71. }
  72. async apply<T>(
  73. key: string,
  74. fn: () => Promise<T>,
  75. opt: CacheApplyOption<T>
  76. ): Promise<T> {
  77. const { ttl, temporaryBypass } = opt;
  78. if (temporaryBypass) {
  79. return fn();
  80. }
  81. if (ttl === null) {
  82. this.del(key);
  83. return fn();
  84. }
  85. const cached = this.get(key);
  86. let value: T;
  87. if (cached == null) {
  88. console.log(picocolors.yellow('[cache] miss'), picocolors.gray(key));
  89. value = await fn();
  90. const serializer = 'serializer' in opt ? opt.serializer : identity;
  91. this.set(key, serializer(value), ttl);
  92. } else {
  93. console.log(picocolors.green('[cache] hit'), picocolors.gray(key));
  94. const deserializer = 'deserializer' in opt ? opt.deserializer : identity;
  95. value = deserializer(cached);
  96. }
  97. return value;
  98. }
  99. destroy() {
  100. this.db.close();
  101. }
  102. }
  103. // export const fsCache = new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') });
  104. // process.on('exit', () => {
  105. // fsCache.destroy();
  106. // });
  107. const separator = String.fromCharCode(0);
  108. export const serializeSet = (set: Set<string>) => Array.from(set).join(separator);
  109. export const deserializeSet = (str: string) => new Set(str.split(separator));