| 65 | } |
| 66 | |
| 67 | export class BethPersistCache { |
| 68 | private callBackMap: Map< |
| 69 | string, |
| 70 | { |
| 71 | callBack: () => Promise<any>; |
| 72 | tags: string[]; |
| 73 | location: "memory" | "json"; |
| 74 | } |
| 75 | >; |
| 76 | private pendingMap: Map<string, Promise<any>>; |
| 77 | private inMemoryDataCache: Map<string, any>; |
| 78 | private jsonDataCache: Database; |
| 79 | private intervals: Set<Timer>; |
| 80 | private keys: Set<string>; |
| 81 | private inInitialLoad: Set<string>; |
| 82 | private config: GlobalCacheConfig; |
| 83 | private neverSeeded: Set<string>; |
| 84 | private toReThrow: Map<string, any>; |
| 85 | |
| 86 | constructor() { |
| 87 | this.callBackMap = new Map(); |
| 88 | this.inMemoryDataCache = new Map(); |
| 89 | this.intervals = new Set(); |
| 90 | this.pendingMap = new Map(); |
| 91 | this.keys = new Set(); |
| 92 | this.inInitialLoad = new Set(); |
| 93 | this.neverSeeded = new Set(); |
| 94 | this.toReThrow = new Map(); |
| 95 | this.config = { |
| 96 | log: false, |
| 97 | defaultCacheOptions: { |
| 98 | persist: "json", |
| 99 | revalidate: Infinity, |
| 100 | tags: [], |
| 101 | seedImmediately: true, |
| 102 | }, |
| 103 | returnStaleWhileRevalidate: true, |
| 104 | onRevalidateErrorReturnStale: true, |
| 105 | rethrowOnUnseededError: false, |
| 106 | }; |
| 107 | |
| 108 | if (existsSync("beth-cache.sqlite")) unlinkSync("beth-cache.sqlite"); |
| 109 | this.jsonDataCache = new Database("beth-cache.sqlite", { |
| 110 | readwrite: true, |
| 111 | create: true, |
| 112 | }); |
| 113 | this.jsonDataCache.run(` |
| 114 | CREATE TABLE IF NOT EXISTS cache ( |
| 115 | key TEXT PRIMARY KEY, |
| 116 | value TEXT NOT NULL |
| 117 | ); |
| 118 | `); |
| 119 | } |
| 120 | |
| 121 | public setConfig(config: Partial<GlobalCacheConfig>) { |
| 122 | this.config.defaultCacheOptions = { |
| 123 | ...this.config.defaultCacheOptions, |
| 124 | ...config.defaultCacheOptions, |
nothing calls this directly
no outgoing calls
no test coverage detected