UNPKG

@pod-protocol/sdk

Version:

TypeScript SDK for PoD Protocol - AI agent communication on Solana

196 lines 5.35 kB
/** * Intelligent caching infrastructure for PoD Protocol SDK * Provides TTL-based caching with invalidation strategies */ /** * Cache utilities for PoD Protocol SDK */ /** * LRU Cache implementation */ export declare class LRUCache<K = string, V = unknown> { private cache; private maxSize; private ttl; private hits; private misses; constructor(maxSize?: number, ttl?: number); get(key: K): V | undefined; set(key: K, value: V): void; delete(key: K): boolean; clear(): void; get size(): number; has(key: K): boolean; getStats(): { hits: number; misses: number; hitRate: number; size: number; }; } export interface CacheEntry<T> { data: T; timestamp: number; ttl: number; accessCount: number; lastAccessed: number; } export interface CacheOptions { defaultTtl?: number; maxSize?: number; checkInterval?: number; } export interface CacheStats { hits: number; misses: number; size: number; hitRate: number; memoryUsage: number; } /** * Generic cache implementation with TTL and LRU eviction */ export declare class Cache<K, V> { private options; private data; private stats; private cleanupTimer?; constructor(options?: CacheOptions); /** * Get value from cache or fetch using provider */ getOrFetch<T extends V>(key: K, fetcher: () => Promise<T>, ttl?: number): Promise<T>; /** * Get value from cache (returns undefined if expired or not found) */ get(key: K): V | undefined; /** * Get value even if expired (for stale-while-revalidate patterns) */ getStale(key: K): V | undefined; /** * Set value in cache with optional TTL */ set(key: K, value: V, ttl?: number): void; /** * Delete specific key from cache */ delete(key: K): boolean; /** * Clear all cached data */ clear(): void; /** * Check if key exists and is not expired */ has(key: K): boolean; /** * Get cache statistics */ getStats(): CacheStats; /** * Get all keys (for debugging) */ keys(): K[]; /** * Invalidate entries matching a pattern or predicate */ invalidate(predicate: (key: K, value: V) => boolean): number; /** * Update TTL for existing entries */ updateTtl(key: K, newTtl: number): boolean; /** * Get entries that will expire soon */ getExpiringSoon(withinMs?: number): Array<{ key: K; value: V; expiresIn: number; }>; /** * Cleanup and destroy cache */ destroy(): void; private isExpired; private evictLRU; private cleanup; private startCleanup; private estimateMemoryUsage; private estimateSize; } /** * Specialized cache for account data */ export declare class AccountCache extends LRUCache { constructor(options?: { defaultTtl?: number; maxSize?: number; } | number); static keys: { account: (address: string) => string; typedAccount: (type: string, address: string) => string; programAccounts: (accountType: string, filters: any[]) => string; }; setAccount(address: string, accountInfo: any): void; getAccount(address: string): any; setTypedAccount(type: string, address: string, account: any): void; getTypedAccount(type: string, address: string): any; setProgramAccounts(accountType: string, filters: any[], accounts: any[]): void; getProgramAccounts(accountType: string, filters: any[]): any[] | undefined; getOrFetch<T>(key: string, fetcher: () => Promise<T>, ttl?: number): Promise<T>; invalidateByType(accountType: string): number; invalidateByProgram(programId: string): number; invalidate(predicate: (key: string) => boolean): number; destroy(): void; } /** * Specialized cache for analytics data */ export declare class AnalyticsCache extends LRUCache { constructor(options?: { defaultTtl?: number; maxSize?: number; } | number); static keys: { agentAnalytics: () => string; agentMetrics: (agentId: string) => string; messageAnalytics: (limit: string) => string; channelAnalytics: (limit: string) => string; networkAnalytics: () => string; }; setAgentAnalytics(analytics: any): void; getAgentAnalytics(): any; setMessageAnalytics(analytics: any, limit: number): void; getMessageAnalytics(limit: number): any; setChannelAnalytics(analytics: any, limit: number): void; getChannelAnalytics(limit: number): any; setNetworkAnalytics(analytics: any): void; getNetworkAnalytics(): any; getOrFetch<T>(key: string, fetcher: () => Promise<T>, ttl?: number): Promise<T>; destroy(): void; } /** * Global cache manager */ export declare class CacheManager { private caches; /** * Get or create a named cache */ getCache<K, V>(name: string, options?: CacheOptions): Cache<K, V>; /** * Clear all caches */ clearAll(): void; /** * Get stats for all caches */ getAllStats(): Record<string, CacheStats>; /** * Destroy all caches */ destroy(): void; } export declare const cacheManager: CacheManager; //# sourceMappingURL=cache.d.ts.map