119 lines
2.8 KiB
TypeScript
119 lines
2.8 KiB
TypeScript
/**
|
|
* SQLite-backed Persistent Cache for Embeddings (sql.js)
|
|
*
|
|
* Features:
|
|
* - Cross-platform support (pure JavaScript/WASM, no native compilation)
|
|
* - Disk persistence across sessions
|
|
* - LRU eviction with configurable max size
|
|
* - Automatic schema creation
|
|
* - TTL support for cache entries
|
|
* - Lazy initialization (no startup cost if not used)
|
|
*/
|
|
/**
|
|
* Configuration for persistent cache
|
|
*/
|
|
export interface PersistentCacheConfig {
|
|
/** Path to SQLite database file */
|
|
dbPath: string;
|
|
/** Maximum number of entries (default: 10000) */
|
|
maxSize?: number;
|
|
/** TTL in milliseconds (default: 7 days) */
|
|
ttlMs?: number;
|
|
/** Enable compression for large embeddings */
|
|
compress?: boolean;
|
|
/** Auto-save interval in ms (default: 30000) */
|
|
autoSaveInterval?: number;
|
|
}
|
|
/**
|
|
* Cache statistics
|
|
*/
|
|
export interface PersistentCacheStats {
|
|
size: number;
|
|
maxSize: number;
|
|
hitRate: number;
|
|
hits: number;
|
|
misses: number;
|
|
dbSizeBytes?: number;
|
|
}
|
|
/**
|
|
* SQLite-backed persistent embedding cache using sql.js (pure JS/WASM)
|
|
*/
|
|
export declare class PersistentEmbeddingCache {
|
|
private db;
|
|
private SQL;
|
|
private initialized;
|
|
private dirty;
|
|
private hits;
|
|
private misses;
|
|
private autoSaveTimer;
|
|
private readonly dbPath;
|
|
private readonly maxSize;
|
|
private readonly ttlMs;
|
|
private readonly autoSaveInterval;
|
|
constructor(config: PersistentCacheConfig);
|
|
/**
|
|
* Lazily initialize database connection
|
|
*/
|
|
private ensureInitialized;
|
|
/**
|
|
* Start auto-save timer
|
|
*/
|
|
private startAutoSave;
|
|
/**
|
|
* Stop auto-save timer
|
|
*/
|
|
private stopAutoSave;
|
|
/**
|
|
* Save database to file
|
|
*/
|
|
private saveToFile;
|
|
/**
|
|
* Generate cache key from text
|
|
*/
|
|
private hashKey;
|
|
/**
|
|
* Serialize Float32Array to Uint8Array for sql.js
|
|
*/
|
|
private serializeEmbedding;
|
|
/**
|
|
* Deserialize Uint8Array to Float32Array
|
|
*/
|
|
private deserializeEmbedding;
|
|
/**
|
|
* Get embedding from cache
|
|
*/
|
|
get(text: string): Promise<Float32Array | null>;
|
|
/**
|
|
* Store embedding in cache
|
|
*/
|
|
set(text: string, embedding: Float32Array): Promise<void>;
|
|
/**
|
|
* Evict oldest entries if cache exceeds max size
|
|
*/
|
|
private evictIfNeeded;
|
|
/**
|
|
* Clean expired entries
|
|
*/
|
|
private cleanExpired;
|
|
/**
|
|
* Get cache statistics
|
|
*/
|
|
getStats(): Promise<PersistentCacheStats>;
|
|
/**
|
|
* Clear all cached entries
|
|
*/
|
|
clear(): Promise<void>;
|
|
/**
|
|
* Force save to disk
|
|
*/
|
|
flush(): Promise<void>;
|
|
/**
|
|
* Close database connection
|
|
*/
|
|
close(): Promise<void>;
|
|
}
|
|
/**
|
|
* Check if persistent cache is available (sql.js installed)
|
|
*/
|
|
export declare function isPersistentCacheAvailable(): Promise<boolean>;
|
|
//# sourceMappingURL=persistent-cache.d.ts.map
|