/** * Cache Key Patterns * Functions to generate consistent cache keys for selective invalidation * * @module cache/patterns */ import type { TimeInterval } from '@plyaz/types/api'; /** * Cache key patterns for organizing cache entries * These generate namespaced keys for better cache organization * * @example * ```typescript * import { cacheKeyPatterns, setCache } from '@plyaz/api'; * * // Create user-specific cache key * const cacheKey = cacheKeyPatterns.byUser('123', 'profile'); * // Returns: "user:123:profile" * * // Use with cache utilities * setCache(cacheKey, userData, { ttl: 300 }); * ``` */ export declare const cacheKeyPatterns: { /** * User-specific cache key * Creates keys like "user:123:endpoint" */ readonly byUser: (userId: string, endpoint: string) => string; /** * Organization-specific cache key * Creates keys like "org:456:endpoint" */ readonly byOrg: (orgId: string, endpoint: string) => string; /** * Generic namespace-based cache key for any resource type * Creates keys like: * - "campaigns:789:stats" * - "nfts:123:metadata" * - "transactions:abc:details" * - "collections:456:items" * * @example * cacheKeyPatterns.byNamespace('campaigns', '789', 'stats') * cacheKeyPatterns.byNamespace('nfts', '123', 'metadata') */ readonly byNamespace: (namespace: string, id: string, endpoint: string) => string; /** * Time-based cache key (for time-series data) * Creates keys like "stats:hour:2024-0-15-14" or "stats:day:2024-0-15" * Uses the time utility for consistent time-based key generation * * @example * cacheKeyPatterns.byTime('analytics', 'hour') * cacheKeyPatterns.byTime('reports', 'week') */ readonly byTime: (endpoint: string, interval: TimeInterval) => string; /** * Version-based cache key * Creates keys like "v2:api:users" */ readonly byVersion: (version: string, endpoint: string) => string; /** * Role-based cache key * Creates keys like "role:admin:dashboard" */ readonly byRole: (role: string, endpoint: string) => string; /** * Session-based cache key * Creates keys like "session:abc123:preferences" */ readonly bySession: (sessionId: string, endpoint: string) => string; /** * Locale-based cache key * Creates keys like "locale:en-US:translations" */ readonly byLocale: (locale: string, endpoint: string) => string; /** * Feature flag cache key * Creates keys like "feature:dark-mode:config" */ readonly byFeature: (feature: string, endpoint: string) => string; /** * Composite cache key with multiple dimensions * Creates keys like "user:123:org:456:project:789" */ readonly composite: (dimensions: Record) => string; /** * Wildcard pattern for bulk operations * Creates patterns like "user:123:*" for deleteCache */ readonly withWildcard: (prefix: string) => string; /** * Global namespace */ readonly global: (endpoint: string) => string; }; /** * Common invalidation scenarios using cache keys * These use cache key patterns for targeted invalidation * * @example * ```typescript * import { invalidationScenarios } from '@plyaz/api'; * * // After any resource update * await invalidationScenarios.afterResourceUpdate('campaigns', 'campaign-789'); * await invalidationScenarios.afterResourceUpdate('user', 'user-123', ['profile', 'settings']); * ``` * * Note: For auth-specific scenarios like login/logout, you can: * - Use afterResourceUpdate('session', sessionId) for session updates * - Use afterResourceUpdate('user', userId) for user cache * - Combine with deleteCache(cacheKeyPatterns.withWildcard('role')) for role changes * - Use forceRefreshAll() for complete cache reset on logout */ export declare const invalidationScenarios: { /** * Generic invalidation after any resource update * Works for campaigns, NFTs, collections, users, sessions, etc. * * @example * afterResourceUpdate('campaigns', 'campaign-123') * afterResourceUpdate('nfts', 'nft-456') * afterResourceUpdate('user', 'user-789', ['profile', 'permissions']) * afterResourceUpdate('session', 'session-abc') */ readonly afterResourceUpdate: (namespace: string, resourceId: string, endpoints?: string[]) => Promise; /** * Invalidate time-based caches (e.g., for stats refresh) * * @param endpoint - The endpoint to refresh * @param intervals - Optional specific intervals to clear (defaults to all common intervals) * * @example * refreshTimeBasedCache('analytics') // Clears all intervals * refreshTimeBasedCache('stats', ['hour', 'day']) // Clears only hour and day */ readonly refreshTimeBasedCache: (endpoint: string, intervals?: TimeInterval[]) => Promise; /** * Force refresh all data * Use sparingly - this is expensive * * Note: Could be enhanced to: * - Accept namespace filters (e.g., only clear 'user:*') * - Support dry-run mode to see what would be cleared * - Add progress callbacks for large cache clears * - Implement staged clearing (clear in chunks) */ readonly forceRefreshAll: () => Promise; }; /** * Create custom cache pattern * Helper for creating type-safe cache patterns * * @example * ```typescript * const customPattern = createCachePattern('/api/custom/:id'); * const key = customPattern({ id: '123' }); // '/api/custom/123' * ``` */ export declare function createCachePattern>(pattern: string): (params: T) => string; /** * Build cache key from parts * Ensures consistent cache key format * * @example * ```typescript * const key = buildCacheKey('users', '123', 'profile'); * // Returns: '/users/123/profile' * ``` */ export declare function buildCacheKey(...parts: string[]): string; //# sourceMappingURL=patterns.d.ts.map