| 50 | * SQLite-backed persistent embedding cache using sql.js (pure JS/WASM) |
| 51 | */ |
| 52 | export class PersistentEmbeddingCache { |
| 53 | private db: SqlJsDatabase | null = null; |
| 54 | private SQL: SqlJsStatic | null = null; |
| 55 | private initialized = false; |
| 56 | private dirty = false; |
| 57 | private hits = 0; |
| 58 | private misses = 0; |
| 59 | private autoSaveTimer: ReturnType<typeof setInterval> | null = null; |
| 60 | |
| 61 | private readonly dbPath: string; |
| 62 | private readonly maxSize: number; |
| 63 | private readonly ttlMs: number; |
| 64 | private readonly autoSaveInterval: number; |
| 65 | |
| 66 | constructor(config: PersistentCacheConfig) { |
| 67 | this.dbPath = config.dbPath; |
| 68 | this.maxSize = config.maxSize ?? 10000; |
| 69 | this.ttlMs = config.ttlMs ?? 7 * 24 * 60 * 60 * 1000; // 7 days |
| 70 | this.autoSaveInterval = config.autoSaveInterval ?? 30000; // 30 seconds |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Lazily initialize database connection |
| 75 | */ |
| 76 | private async ensureInitialized(): Promise<void> { |
| 77 | if (this.initialized) return; |
| 78 | |
| 79 | try { |
| 80 | // Dynamically import sql.js |
| 81 | const initSqlJs = (await import('sql.js')).default; |
| 82 | |
| 83 | // Initialize sql.js (loads WASM) |
| 84 | this.SQL = await initSqlJs(); |
| 85 | |
| 86 | // Ensure directory exists |
| 87 | const dir = dirname(this.dbPath); |
| 88 | if (!existsSync(dir)) { |
| 89 | mkdirSync(dir, { recursive: true }); |
| 90 | } |
| 91 | |
| 92 | // Load existing database or create new |
| 93 | if (existsSync(this.dbPath)) { |
| 94 | const fileBuffer = readFileSync(this.dbPath); |
| 95 | this.db = new this.SQL.Database(fileBuffer); |
| 96 | } else { |
| 97 | this.db = new this.SQL.Database(); |
| 98 | } |
| 99 | |
| 100 | // Create schema |
| 101 | this.db.run(` |
| 102 | CREATE TABLE IF NOT EXISTS embeddings ( |
| 103 | key TEXT PRIMARY KEY, |
| 104 | embedding BLOB NOT NULL, |
| 105 | dimensions INTEGER NOT NULL, |
| 106 | created_at INTEGER NOT NULL, |
| 107 | accessed_at INTEGER NOT NULL, |
| 108 | access_count INTEGER DEFAULT 1 |
| 109 | ) |
nothing calls this directly
no outgoing calls
no test coverage detected