Buckets:
| import { logger } from './logger.js'; | |
| export const CACHE_CONFIG = { | |
| SPACE_METADATA_TTL: parseInt(process.env.GRADIO_SPACE_CACHE_TTL || '300000', 10), | |
| SCHEMA_TTL: parseInt(process.env.GRADIO_SCHEMA_CACHE_TTL || '300000', 10), | |
| DISCOVERY_CONCURRENCY: parseInt(process.env.GRADIO_DISCOVERY_CONCURRENCY || '10', 10), | |
| SPACE_INFO_TIMEOUT: parseInt(process.env.GRADIO_SPACE_INFO_TIMEOUT || '5000', 10), | |
| SCHEMA_TIMEOUT: parseInt(process.env.GRADIO_SCHEMA_TIMEOUT || '7500', 10), | |
| }; | |
| class SpaceMetadataCache { | |
| cache = new Map(); | |
| stats = { | |
| hits: 0, | |
| misses: 0, | |
| etagRevalidations: 0, | |
| }; | |
| get(spaceName) { | |
| const entry = this.cache.get(spaceName); | |
| if (!entry) { | |
| this.stats.misses++; | |
| logger.trace({ spaceName }, 'Space metadata cache miss'); | |
| return null; | |
| } | |
| const age = Date.now() - entry.fetchedAt; | |
| const isValid = age < CACHE_CONFIG.SPACE_METADATA_TTL; | |
| if (!isValid) { | |
| this.stats.misses++; | |
| logger.trace({ spaceName, age, ttl: CACHE_CONFIG.SPACE_METADATA_TTL }, 'Space metadata cache expired'); | |
| return null; | |
| } | |
| this.stats.hits++; | |
| logger.trace({ spaceName, age }, 'Space metadata cache hit'); | |
| return entry; | |
| } | |
| getForRevalidation(spaceName) { | |
| return this.cache.get(spaceName) || null; | |
| } | |
| set(spaceName, metadata) { | |
| this.cache.set(spaceName, metadata); | |
| logger.trace({ spaceName, hasEtag: !!metadata.etag }, 'Space metadata cached'); | |
| } | |
| updateTimestamp(spaceName) { | |
| const entry = this.cache.get(spaceName); | |
| if (entry) { | |
| entry.fetchedAt = Date.now(); | |
| this.stats.etagRevalidations++; | |
| logger.trace({ spaceName }, 'Space metadata timestamp updated after 304'); | |
| } | |
| } | |
| clear() { | |
| this.cache.clear(); | |
| this.stats.hits = 0; | |
| this.stats.misses = 0; | |
| this.stats.etagRevalidations = 0; | |
| logger.debug('Space metadata cache cleared'); | |
| } | |
| getStats() { | |
| return { | |
| hits: this.stats.hits, | |
| misses: this.stats.misses, | |
| etagRevalidations: this.stats.etagRevalidations, | |
| size: this.cache.size, | |
| }; | |
| } | |
| } | |
| class SchemaCache { | |
| cache = new Map(); | |
| stats = { | |
| hits: 0, | |
| misses: 0, | |
| }; | |
| get(spaceName) { | |
| const entry = this.cache.get(spaceName); | |
| if (!entry) { | |
| this.stats.misses++; | |
| logger.trace({ spaceName }, 'Schema cache miss'); | |
| return null; | |
| } | |
| const age = Date.now() - entry.fetchedAt; | |
| const isValid = age < CACHE_CONFIG.SCHEMA_TTL; | |
| if (!isValid) { | |
| this.stats.misses++; | |
| logger.trace({ spaceName, age, ttl: CACHE_CONFIG.SCHEMA_TTL }, 'Schema cache expired'); | |
| return null; | |
| } | |
| this.stats.hits++; | |
| logger.trace({ spaceName, age, toolCount: entry.tools.length }, 'Schema cache hit'); | |
| return entry; | |
| } | |
| set(spaceName, schema) { | |
| this.cache.set(spaceName, schema); | |
| logger.trace({ spaceName, toolCount: schema.tools.length }, 'Schema cached'); | |
| } | |
| clear() { | |
| this.cache.clear(); | |
| this.stats.hits = 0; | |
| this.stats.misses = 0; | |
| logger.debug('Schema cache cleared'); | |
| } | |
| getStats() { | |
| return { | |
| hits: this.stats.hits, | |
| misses: this.stats.misses, | |
| size: this.cache.size, | |
| }; | |
| } | |
| } | |
| export const spaceMetadataCache = new SpaceMetadataCache(); | |
| export const schemaCache = new SchemaCache(); | |
| export function getCacheStats() { | |
| const metadataStats = spaceMetadataCache.getStats(); | |
| const schemaStats = schemaCache.getStats(); | |
| return { | |
| metadataHits: metadataStats.hits, | |
| metadataMisses: metadataStats.misses, | |
| metadataEtagRevalidations: metadataStats.etagRevalidations, | |
| schemaHits: schemaStats.hits, | |
| schemaMisses: schemaStats.misses, | |
| metadataCacheSize: metadataStats.size, | |
| schemaCacheSize: schemaStats.size, | |
| }; | |
| } | |
| export function clearAllCaches() { | |
| spaceMetadataCache.clear(); | |
| schemaCache.clear(); | |
| logger.info('All Gradio caches cleared'); | |
| } | |
| export function logCacheStats() { | |
| const stats = getCacheStats(); | |
| const metadataHitRate = stats.metadataHits + stats.metadataMisses > 0 | |
| ? ((stats.metadataHits / (stats.metadataHits + stats.metadataMisses)) * 100).toFixed(1) | |
| : '0.0'; | |
| const schemaHitRate = stats.schemaHits + stats.schemaMisses > 0 | |
| ? ((stats.schemaHits / (stats.schemaHits + stats.schemaMisses)) * 100).toFixed(1) | |
| : '0.0'; | |
| logger.debug({ | |
| metadata: { | |
| hits: stats.metadataHits, | |
| misses: stats.metadataMisses, | |
| etagRevalidations: stats.metadataEtagRevalidations, | |
| hitRate: `${metadataHitRate}%`, | |
| cacheSize: stats.metadataCacheSize, | |
| }, | |
| schema: { | |
| hits: stats.schemaHits, | |
| misses: stats.schemaMisses, | |
| hitRate: `${schemaHitRate}%`, | |
| cacheSize: stats.schemaCacheSize, | |
| }, | |
| }, 'Gradio cache statistics'); | |
| } | |
| export function formatCacheMetricsForAPI() { | |
| const stats = getCacheStats(); | |
| const metadataTotal = stats.metadataHits + stats.metadataMisses; | |
| const metadataHitRate = metadataTotal > 0 | |
| ? Math.round((stats.metadataHits / metadataTotal) * 10000) / 100 | |
| : 0; | |
| const schemaTotal = stats.schemaHits + stats.schemaMisses; | |
| const schemaHitRate = schemaTotal > 0 | |
| ? Math.round((stats.schemaHits / schemaTotal) * 10000) / 100 | |
| : 0; | |
| const totalHits = stats.metadataHits + stats.schemaHits; | |
| const totalMisses = stats.metadataMisses + stats.schemaMisses; | |
| const grandTotal = totalHits + totalMisses; | |
| const overallHitRate = grandTotal > 0 | |
| ? Math.round((totalHits / grandTotal) * 10000) / 100 | |
| : 0; | |
| return { | |
| spaceMetadata: { | |
| hits: stats.metadataHits, | |
| misses: stats.metadataMisses, | |
| hitRate: metadataHitRate, | |
| etagRevalidations: stats.metadataEtagRevalidations, | |
| cacheSize: stats.metadataCacheSize, | |
| }, | |
| schemas: { | |
| hits: stats.schemaHits, | |
| misses: stats.schemaMisses, | |
| hitRate: schemaHitRate, | |
| cacheSize: stats.schemaCacheSize, | |
| }, | |
| totalHits, | |
| totalMisses, | |
| overallHitRate, | |
| }; | |
| } | |
| //# sourceMappingURL=gradio-cache.js.map |
Xet Storage Details
- Size:
- 6.55 kB
- Xet hash:
- ff69cbe3f75fdd8ffddb357b42ca329ac2f72ac122c865beb863027e4a13ba11
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.