| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 55583c4 commit 54d712f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,8 @@ | |||
| 6 | 6 | * found in the LICENSE file at https://angular.dev/license | |
| 7 | 7 | */ | |
| 8 | 8 | ||
| 9 | + import { mkdirSync } from 'node:fs'; | ||
| 10 | + import { dirname } from 'node:path'; | ||
| 9 | 11 | import { DatabaseSync, StatementSync } from 'node:sqlite'; | |
| 10 | 12 | import { deserialize, serialize } from 'node:v8'; | |
| 11 | 13 | import { Cache, PersistentCacheStore } from './cache'; | |
@@ -35,7 +37,19 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 35 | 37 | ||
| 36 | 38 | #ensureDb(): DatabaseSync { | |
| 37 | 39 | if (!this.#db) { | |
| 38 | - this.#db = new DatabaseSync(this.cachePath); | ||
| 40 | + if (this.cachePath === ':memory:') { | ||
| 41 | + this.#db = new DatabaseSync(this.cachePath); | ||
| 42 | + } else { | ||
| 43 | + // Optimistically attempt to open the database file first to avoid directory creation | ||
| 44 | + // syscalls on warm builds where the parent directory already exists. | ||
| 45 | + try { | ||
| 46 | + this.#db = new DatabaseSync(this.cachePath); | ||
| 47 | + } catch { | ||
| 48 | + mkdirSync(dirname(this.cachePath), { recursive: true }); | ||
| 49 | + this.#db = new DatabaseSync(this.cachePath); | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + | ||
| 39 | 53 | // Optimize SQLite for cache usage | |
| 40 | 54 | this.#db.exec('PRAGMA auto_vacuum = FULL;'); | |
| 41 | 55 | this.#db.exec('PRAGMA journal_mode = WAL;'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -220,6 +220,31 @@ describe('SqliteCacheStore', () => { | |||
| 220 | 220 | } | |
| 221 | 221 | }); | |
| 222 | 222 | ||
| 223 | + it('should create parent directories if they do not exist', async () => { | ||
| 224 | + const nestedDir = join(tempDir, 'nested', 'deeply', 'cache'); | ||
| 225 | + const nestedCachePath = join(nestedDir, 'nested-cache.db'); | ||
| 226 | + const nestedStore = new SqliteCacheStore(nestedCachePath); | ||
| 227 | + | ||
| 228 | + try { | ||
| 229 | + await nestedStore.set('nested-key', 'nested-value'); | ||
| 230 | + const result = await nestedStore.get('nested-key'); | ||
| 231 | + expect(result).toBe('nested-value'); | ||
| 232 | + } finally { | ||
| 233 | + nestedStore.close(); | ||
| 234 | + } | ||
| 235 | + }); | ||
| 236 | + | ||
| 237 | + it('should support in-memory databases', async () => { | ||
| 238 | + const memoryStore = new SqliteCacheStore(':memory:'); | ||
| 239 | + try { | ||
| 240 | + await memoryStore.set('mem-key', 'mem-value'); | ||
| 241 | + const result = await memoryStore.get('mem-key'); | ||
| 242 | + expect(result).toBe('mem-value'); | ||
| 243 | + } finally { | ||
| 244 | + memoryStore.close(); | ||
| 245 | + } | ||
| 246 | + }); | ||
| 247 | + | ||
| 223 | 248 | describe('NG_BUILD_CACHE_STORE env variable option', () => { | |
| 224 | 249 | it('should force SQLite when NG_BUILD_CACHE_STORE=sqlite', () => { | |
| 225 | 250 | const code = ` | |
| Back | FazBrowse Home | New Git URL |
0 commit comments