|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { DatabaseSync, StatementSync } from 'node:sqlite'; |
| 10 | +import { Cache, PersistentCacheStore } from './cache'; |
| 11 | + |
| 12 | +export class SqliteCacheStore implements PersistentCacheStore<unknown> { |
| 13 | + #db: DatabaseSync | undefined; |
| 14 | + #getStmt: StatementSync | undefined; |
| 15 | + #hasStmt: StatementSync | undefined; |
| 16 | + #setStmt: StatementSync | undefined; |
| 17 | + #updateAccessedStmt: StatementSync | undefined; |
| 18 | + |
| 19 | + constructor( |
| 20 | + readonly cachePath: string, |
| 21 | + private readonly maxPayloadSize = 1024 * 1024 * 1024, |
| 22 | + private readonly ttlDays = 14, |
| 23 | + ) {} |
| 24 | + |
| 25 | + #ensureDb(): DatabaseSync { |
| 26 | + if (!this.#db) { |
| 27 | + this.#db = new DatabaseSync(this.cachePath); |
| 28 | + // Optimize SQLite for cache usage |
| 29 | + this.#db.exec('PRAGMA auto_vacuum = FULL;'); |
| 30 | + this.#db.exec('PRAGMA journal_mode = WAL;'); |
| 31 | + this.#db.exec('PRAGMA synchronous = NORMAL;'); |
| 32 | + this.#db.exec( |
| 33 | + 'CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, value TEXT, last_accessed INTEGER NOT NULL) WITHOUT ROWID;', |
| 34 | + ); |
| 35 | + |
| 36 | + this.#getStmt = this.#db.prepare('SELECT value FROM cache WHERE key = ?'); |
| 37 | + this.#hasStmt = this.#db.prepare('SELECT 1 FROM cache WHERE key = ?'); |
| 38 | + this.#setStmt = this.#db.prepare( |
| 39 | + 'INSERT OR REPLACE INTO cache (key, value, last_accessed) VALUES (?, ?, unixepoch())', |
| 40 | + ); |
| 41 | + this.#updateAccessedStmt = this.#db.prepare( |
| 42 | + 'UPDATE cache SET last_accessed = unixepoch() WHERE key = ?', |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return this.#db; |
| 47 | + } |
| 48 | + |
| 49 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 50 | + async get(key: string): Promise<any> { |
| 51 | + this.#ensureDb(); |
| 52 | + const row = this.#getStmt?.get(key) as { value: string } | undefined; |
| 53 | + |
| 54 | + if (row) { |
| 55 | + this.#updateAccessedStmt?.run(key); |
| 56 | + |
| 57 | + try { |
| 58 | + return JSON.parse(row.value); |
| 59 | + } catch { |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + |
| 67 | + has(key: string): boolean { |
| 68 | + this.#ensureDb(); |
| 69 | + |
| 70 | + return !!this.#hasStmt?.get(key); |
| 71 | + } |
| 72 | + |
| 73 | + async set(key: string, value: unknown): Promise<this> { |
| 74 | + this.#ensureDb(); |
| 75 | + this.#setStmt?.run(key, JSON.stringify(value)); |
| 76 | + |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + createCache<V = unknown>(namespace: string): Cache<V> { |
| 81 | + return new Cache(this, namespace); |
| 82 | + } |
| 83 | + |
| 84 | + close(): void { |
| 85 | + if (this.#db) { |
| 86 | + try { |
| 87 | + // 1. Delete items older than N days |
| 88 | + this.#db |
| 89 | + .prepare("DELETE FROM cache WHERE last_accessed < unixepoch('now', ?);") |
| 90 | + .run(`-${this.ttlDays} days`); |
| 91 | + |
| 92 | + // 2. Prune oldest items if payload exceeds maxPayloadSize |
| 93 | + const pruneStmt = this.#db.prepare(` |
| 94 | + DELETE FROM cache WHERE key IN ( |
| 95 | + SELECT key FROM ( |
| 96 | + SELECT key, |
| 97 | + sum(length(key) + length(value)) OVER (ORDER BY last_accessed DESC, key DESC) as running_size |
| 98 | + FROM cache |
| 99 | + ) WHERE running_size > ? |
| 100 | + ); |
| 101 | + `); |
| 102 | + pruneStmt.run(this.maxPayloadSize); |
| 103 | + } catch { |
| 104 | + // Pruning errors should not block build success |
| 105 | + } finally { |
| 106 | + this.#getStmt = undefined; |
| 107 | + this.#hasStmt = undefined; |
| 108 | + this.#setStmt = undefined; |
| 109 | + this.#updateAccessedStmt = undefined; |
| 110 | + |
| 111 | + this.#db.close(); |
| 112 | + this.#db = undefined; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments