/** * @fileoverview CVE Cache with SQLite storage * @module @nahisaho/musubix-security/cve/cve-cache * @description Provides persistent caching for CVE data to reduce NVD API calls * @requirements REQ-SEC-CVE-002 - Rate limiting and caching for NVD API * @design DES-SEC-CVE-002 - CVE cache with configurable TTL * @task TSK-CVE-007 - SQLite キャッシュ */ import type { CVE } from '../types/cve.js'; /** * Cache entry with metadata */ export interface CacheEntry { /** Cached data */ data: T; /** Timestamp when the entry was created */ createdAt: number; /** Timestamp when the entry expires */ expiresAt: number; /** Optional ETag for conditional requests */ etag?: string; } /** * CVE Cache configuration options */ export interface CVECacheOptions { /** Path to the SQLite database file */ dbPath?: string; /** Default TTL in milliseconds (default: 24 hours) */ defaultTTL?: number; /** Maximum number of entries (default: 10000) */ maxEntries?: number; /** Enable automatic cleanup (default: true) */ autoCleanup?: boolean; /** Cleanup interval in milliseconds (default: 1 hour) */ cleanupInterval?: number; } /** * Cache statistics */ export interface CacheStats { /** Total number of entries */ totalEntries: number; /** Number of expired entries */ expiredEntries: number; /** Number of valid entries */ validEntries: number; /** Cache hit rate */ hitRate: number; /** Total cache size in bytes */ sizeBytes: number; /** Oldest entry timestamp */ oldestEntry: number | null; /** Newest entry timestamp */ newestEntry: number | null; } /** * CVE Cache implementation using file-based JSON storage * @description Uses JSON files for persistence to avoid native module dependencies */ export declare class CVECache { private readonly dbPath; private readonly defaultTTL; private readonly maxEntries; private readonly autoCleanup; private readonly cleanupInterval; private cleanupTimer?; private metrics; private cache; /** * Default 24 hours TTL */ static readonly DEFAULT_TTL: number; /** * Default maximum entries */ static readonly DEFAULT_MAX_ENTRIES = 10000; /** * Default cleanup interval (1 hour) */ static readonly DEFAULT_CLEANUP_INTERVAL: number; constructor(options?: CVECacheOptions); /** * Initialize storage directory and file */ private initializeStorage; /** * Load cache from disk */ private loadFromDisk; /** * Save cache to disk */ private saveToDisk; /** * Start automatic cleanup timer */ private startCleanupTimer; /** * Stop automatic cleanup timer */ private stopCleanupTimer; /** * Generate cache key for CVE ID */ private cveKey; /** * Generate cache key for CPE search */ private cpeKey; /** * Generate cache key for keyword search */ private keywordKey; /** * Get a CVE from cache * @param cveId - CVE identifier (e.g., CVE-2024-12345) * @returns Cached CVE or undefined if not found/expired */ getCVE(cveId: string): CVE | undefined; /** * Store a CVE in cache * @param cve - CVE to cache * @param ttl - Optional custom TTL in milliseconds * @param etag - Optional ETag for conditional requests */ setCVE(cve: CVE, ttl?: number, etag?: string): void; /** * Get CVEs for a CPE from cache * @param cpeName - CPE name string * @returns Cached CVE array or undefined */ getCVEsByCPE(cpeName: string): CVE[] | undefined; /** * Store CVEs for a CPE in cache * @param cpeName - CPE name string * @param cves - Array of CVEs to cache * @param ttl - Optional custom TTL */ setCVEsByCPE(cpeName: string, cves: CVE[], ttl?: number): void; /** * Get CVEs for a keyword search from cache * @param keyword - Search keyword * @returns Cached CVE array or undefined */ getCVEsByKeyword(keyword: string): CVE[] | undefined; /** * Store CVEs for a keyword search in cache * @param keyword - Search keyword * @param cves - Array of CVEs to cache * @param ttl - Optional custom TTL (shorter recommended for searches) */ setCVEsByKeyword(keyword: string, cves: CVE[], ttl?: number): void; /** * Check if a CVE is in cache and not expired * @param cveId - CVE identifier * @returns true if cached and valid */ hasCVE(cveId: string): boolean; /** * Delete a CVE from cache * @param cveId - CVE identifier * @returns true if entry was deleted */ deleteCVE(cveId: string): boolean; /** * Get cache entry metadata * @param cveId - CVE identifier * @returns Cache entry metadata or undefined */ getCVEMetadata(cveId: string): Omit, 'data'> | undefined; /** * Remove expired entries * @returns Number of entries removed */ cleanup(): number; /** * Enforce maximum entries limit (LRU eviction) */ private enforceMaxEntries; /** * Clear all entries */ clear(): void; /** * Get cache statistics */ getStats(): CacheStats; /** * Close cache and cleanup resources */ close(): void; /** * Batch get multiple CVEs * @param cveIds - Array of CVE identifiers * @returns Map of CVE ID to CVE (only found entries) */ getMultipleCVEs(cveIds: string[]): Map; /** * Batch set multiple CVEs * @param cves - Array of CVEs to cache * @param ttl - Optional custom TTL */ setMultipleCVEs(cves: CVE[], ttl?: number): void; /** * Touch a cache entry to extend its TTL * @param cveId - CVE identifier * @param ttl - Optional new TTL * @returns true if entry was touched */ touchCVE(cveId: string, ttl?: number): boolean; } /** * Create a CVE cache with memory-only storage (no persistence) */ export declare function createMemoryCache(options?: Omit): CVECache; /** * Get or create the default cache instance */ export declare function getDefaultCache(): CVECache; /** * Close the default cache instance */ export declare function closeDefaultCache(): void; //# sourceMappingURL=cve-cache.d.ts.map