/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/google/cache * * Reinforced SQLite-backed cache for Google SERP / fetch results. * * Features: * - High-performance Bun native SQLite driver (Zig-powered). * - WAL (Write-Ahead Logging) enabled for concurrent read/write. * - Native BLOB support for binary snapshots (compressed HTML/images). * - Strict parameter binding. * - Automatic eviction (LRU-ish based on creation time). */ import { Database } from "bun:sqlite"; export interface CacheOptions { /** * Path to sqlite file. * Defaults to `~/.bxc/cache.sqlite` or `:memory:`. */ path?: string; /** Default TTL in ms. Defaults to 24h. */ defaultTtlMs?: number; /** Cap entries (LRU-style purge). Defaults to 10000. */ maxEntries?: number; /** Enable strict mode for binding. */ strict?: boolean; } const DEFAULT_TTL_MS = 24 * 60 * 60 * 1000; const DEFAULT_MAX_ENTRIES = 10000; import { getCacheFile } from "../utils/paths.ts"; export class GoogleCache { readonly #db: Database; readonly #ttl: number; readonly #max: number; #closed = false; // Prepared statements for maximum speed (compiled once) readonly #getStmt: any; readonly #setStmt: any; readonly #delStmt: any; readonly #purgeStmt: any; readonly #countStmt: any; readonly #evictStmt: any; constructor(opts: CacheOptions = {}) { let dbPath = opts.path; if (!dbPath) { try { dbPath = getCacheFile("cache.sqlite"); } catch { dbPath = ":memory:"; } } this.#db = new Database(dbPath ?? ":memory:", { create: true, strict: opts.strict ?? true, }); // WAL mode is crucial for performance on VPS with multiple workers this.#db.exec(` PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA temp_store = MEMORY; PRAGMA cache_size = -10000; `); this.#db.exec(` CREATE TABLE IF NOT EXISTS cache ( key TEXT PRIMARY KEY, payload BLOB NOT NULL, is_json INTEGER NOT NULL DEFAULT 0, created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_cache_expires ON cache(expires_at); `); // Safely migrate schema to include compression column try { this.#db.exec( "ALTER TABLE cache ADD COLUMN compression INTEGER NOT NULL DEFAULT 0;", ); } catch { // Column already exists } this.#ttl = opts.defaultTtlMs ?? DEFAULT_TTL_MS; this.#max = opts.maxEntries ?? DEFAULT_MAX_ENTRIES; this.#getStmt = this.#db.prepare( "SELECT payload, is_json, compression FROM cache WHERE key = ? AND expires_at > ?", ); this.#setStmt = this.#db.prepare( "INSERT OR REPLACE INTO cache (key, payload, is_json, created_at, expires_at, compression) VALUES (?, ?, ?, ?, ?, ?)", ); this.#delStmt = this.#db.prepare("DELETE FROM cache WHERE key = ?"); this.#purgeStmt = this.#db.prepare( "DELETE FROM cache WHERE expires_at < ?", ); this.#countStmt = this.#db.prepare("SELECT COUNT(*) AS n FROM cache"); this.#evictStmt = this.#db.prepare( "DELETE FROM cache WHERE key IN (SELECT key FROM cache ORDER BY created_at ASC LIMIT ?)", ); // Schedule a background purge of expired items on launch setTimeout(() => { try { this.purgeExpired(); } catch { // ignore } }, 1000); } get(key: string): T | null { const row = this.#getStmt.get(key, Date.now()) as { payload: Uint8Array | string; is_json: number; compression: number; } | null; if (!row) return null; let binary = row.payload; if (row.compression === 1) { binary = Bun.gunzipSync(binary as any); } if (row.is_json === 1) { try { const text = typeof binary === "string" ? binary : new TextDecoder().decode(binary); return JSON.parse(text) as T; } catch { return null; } } if (row.is_json === 2) { return (typeof binary === "string" ? binary : new TextDecoder().decode(binary)) as unknown as T; } return binary as unknown as T; } /** * Store a value in the cache with an optional TTL. * Automatically detects if the value should be stored as JSON or raw BLOB. * Compresses values dynamically if they exceed 512 bytes. */ set(key: string, value: T, ttlMs?: number): void { const now = Date.now(); const expires = now + (ttlMs ?? this.#ttl); let payload: Uint8Array | string; let isJson = 0; // 0 = raw, 1 = json, 2 = string let compression = 0; if (value instanceof Uint8Array) { payload = value; isJson = 0; } else if (typeof value === "string") { payload = value; isJson = 2; } else { payload = JSON.stringify(value); isJson = 1; } if (payload.length > 512) { const rawBytes = typeof payload === "string" ? new TextEncoder().encode(payload) : payload; payload = Bun.gzipSync(rawBytes as any); compression = 1; } this.#setStmt.run(key, payload, isJson, now, expires, compression); this.#maybeEvict(); } delete(key: string): void { this.#delStmt.run(key); } /** * Remove all expired entries. Returns number of removed rows. */ purgeExpired(): number { const res = this.#purgeStmt.run(Date.now()); return res.changes; } /** * Returns the current number of entries in the cache. */ size(): number { const row = this.#countStmt.get() as { n: number }; return row.n; } /** * Force a WAL checkpoint and close the database. */ close(): void { if (this.#closed) return; this.#closed = true; this.#getStmt.finalize(); this.#setStmt.finalize(); this.#delStmt.finalize(); this.#purgeStmt.finalize(); this.#countStmt.finalize(); this.#evictStmt.finalize(); this.#db.exec("PRAGMA wal_checkpoint(TRUNCATE);"); this.#db.close(); } #maybeEvict(): void { const n = this.size(); if (n <= this.#max) return; this.#evictStmt.run(n - this.#max); } } let _shared: GoogleCache | null = null; /** * Global reinforced cache instance for the Bxc process. */ export function sharedCache(): GoogleCache { if (!_shared) _shared = new GoogleCache(); return _shared; }