/** * Create a QueryCache instance with default options * * @param {QueryCacheOptions} [options] - Cache options * @returns {QueryCache} New QueryCache instance */ export function createQueryCache(options?: QueryCacheOptions): QueryCache; /** * Cache entry with metadata for LRU tracking and TTL * @typedef {Object} CacheEntry * @property {any} result - The cached query result * @property {number} timestamp - When the entry was created * @property {number} expiry - When the entry expires (timestamp + ttl) * @property {string} queryHash - Hash of the query for identification * @property {Set} patterns - Triple patterns affected by this query */ /** * Options for QueryCache constructor * @typedef {Object} QueryCacheOptions * @property {number} [maxSize=100] - Maximum number of cached entries * @property {number} [ttl=60000] - Time-to-live in milliseconds (default: 1 minute) */ /** * Cache statistics * @typedef {Object} CacheStats * @property {number} hits - Number of cache hits * @property {number} misses - Number of cache misses * @property {number} evictions - Number of LRU evictions * @property {number} expirations - Number of TTL expirations * @property {number} size - Current cache size * @property {number} hitRate - Hit rate (hits / (hits + misses)) */ /** * LRU Cache for SPARQL query results * * Uses Map's insertion order for LRU tracking. * Entries are moved to the end on access (most recently used). * Eviction removes from the beginning (least recently used). */ export class QueryCache { /** * Create a new QueryCache * * @param {QueryCacheOptions} [options={}] - Cache configuration */ constructor(options?: QueryCacheOptions); /** @type {number} */ maxSize: number; /** @type {number} */ ttl: number; /** @type {Map} */ _cache: Map; /** @type {number} */ _hits: number; /** @type {number} */ _misses: number; /** @type {number} */ _evictions: number; /** @type {number} */ _expirations: number; _metricsInitialized: boolean; _hitCounter: import("@opentelemetry/api").Counter; _missCounter: import("@opentelemetry/api").Counter; _evictionCounter: import("@opentelemetry/api").Counter; /** * Initialize OTEL metrics lazily * @private */ private _initMetrics; /** * Generate cache key from query and bindings * * @param {string} query - SPARQL query string * @param {Object} [bindings={}] - Variable bindings * @returns {string} Cache key * @private */ private _generateKey; /** * Extract triple patterns from a SPARQL query for invalidation tracking * * @param {string} query - SPARQL query string * @returns {Set} Set of pattern signatures * @private */ private _extractPatterns; /** * Check if an entry has expired * * @param {CacheEntry} entry - Cache entry to check * @returns {boolean} True if expired * @private */ private _isExpired; /** * Move entry to end of Map (most recently used) * * @param {string} key - Cache key * @param {CacheEntry} entry - Cache entry * @private */ private _touch; /** * Evict least recently used entry * * @private */ private _evictLRU; /** * Cache a query result * * @param {string} query - SPARQL query string * @param {Object} bindings - Variable bindings used in query * @param {any} result - Query result to cache */ set(query: string, bindings: any, result: any): void; /** * Retrieve a cached query result * * @param {string} query - SPARQL query string * @param {Object} [bindings={}] - Variable bindings * @returns {any|undefined} Cached result or undefined if not found/expired */ get(query: string, bindings?: any): any | undefined; /** * Invalidate cache entries matching a triple pattern * * Pattern format: { subject: uri|'*', predicate: uri|'*', object: uri|'*' } * Use '*' for wildcard matching any value. * * @param {Object} pattern - Triple pattern for invalidation * @param {string} [pattern.subject='*'] - Subject URI or '*' for any * @param {string} [pattern.predicate='*'] - Predicate URI or '*' for any * @param {string} [pattern.object='*'] - Object value or '*' for any * @returns {number} Number of entries invalidated */ invalidate(pattern: { subject?: string; predicate?: string; object?: string; }): number; /** * Check if two pattern signatures match * * @param {string} pattern1 - First pattern * @param {string} pattern2 - Second pattern * @returns {boolean} True if patterns match * @private */ private _patternsMatch; /** * Clear all cached entries */ invalidateAll(): number; /** * Get cache statistics * * @returns {CacheStats} Cache statistics */ stats(): CacheStats; /** * Check if cache contains an entry for query (without affecting stats) * * @param {string} query - SPARQL query string * @param {Object} [bindings={}] - Variable bindings * @returns {boolean} True if entry exists and is not expired */ has(query: string, bindings?: any): boolean; /** * Get current cache size * * @returns {number} Number of entries in cache */ get size(): number; /** * Clean up expired entries * * @returns {number} Number of entries removed */ prune(): number; } export default QueryCache; /** * Cache entry with metadata for LRU tracking and TTL */ export type CacheEntry = { /** * - The cached query result */ result: any; /** * - When the entry was created */ timestamp: number; /** * - When the entry expires (timestamp + ttl) */ expiry: number; /** * - Hash of the query for identification */ queryHash: string; /** * - Triple patterns affected by this query */ patterns: Set; }; /** * Options for QueryCache constructor */ export type QueryCacheOptions = { /** * - Maximum number of cached entries */ maxSize?: number; /** * - Time-to-live in milliseconds (default: 1 minute) */ ttl?: number; }; /** * Cache statistics */ export type CacheStats = { /** * - Number of cache hits */ hits: number; /** * - Number of cache misses */ misses: number; /** * - Number of LRU evictions */ evictions: number; /** * - Number of TTL expirations */ expirations: number; /** * - Current cache size */ size: number; /** * - Hit rate (hits / (hits + misses)) */ hitRate: number; };