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AAc,GAAG,CAAC,CAAC;YACnD,CAAC;iBAAM,CAAC;gBACN,aAAK,CAAC,GAAG,CAAC,YAAY,cAAc,GAAG,CAAC,CAAC;YAC3C,CAAC;YAED,YAAY,GAAG;gBACb,UAAU;gBACV,eAAe,EAAE,KAAK;gBACtB,UAAU,EAAE,KAAK;aAClB,CAAC;YAEF,WAAW,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACzD,CAAC;QAED,OAAO,YAAY,CAAC,UAAU,CAAC;IACjC,CAAC;;AA9EH,kCA+EC;AA9EC,uEAAuE;AACxD,0BAAc,GAA+B,IAAI,GAAG,EAAyB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { TSDocConfigFile } from '@microsoft/tsdoc-config';\r\nimport * as path from 'path';\r\n\r\nimport { Debug } from './Debug';\r\n\r\ninterface ICachedConfig {\r\n loadTimeMs: number;\r\n lastCheckTimeMs: number;\r\n configFile: TSDocConfigFile;\r\n}\r\n\r\n// How often to check for modified input files. If a file's modification timestamp has changed, then we will\r\n// evict the cache entry immediately.\r\nconst CACHE_CHECK_INTERVAL_MS: number = 3 * 1000;\r\n\r\n// Evict old entries from the cache after this much time, regardless of whether the file was detected as being\r\n// modified or not.\r\nconst CACHE_EXPIRE_MS: number = 20 * 1000;\r\n\r\n// If this many objects accumulate in the cache, then it is cleared to avoid a memory leak.\r\nconst CACHE_MAX_SIZE: number = 100;\r\n\r\nexport class ConfigCache {\r\n // findConfigPathForFolder() result --> loaded tsdoc.json configuration\r\n private static _cachedConfigs: Map<string, ICachedConfig> = new Map<string, ICachedConfig>();\r\n\r\n /**\r\n * Node.js equivalent of performance.now().\r\n */\r\n private static _getTimeInMs(): number {\r\n const [seconds, nanoseconds] = process.hrtime();\r\n return seconds * 1000 + nanoseconds / 1000000;\r\n }\r\n\r\n public static getForSourceFile(sourceFilePath: string): TSDocConfigFile {\r\n const sourceFileFolder: string = path.dirname(path.resolve(sourceFilePath));\r\n\r\n // First, determine the file to be loaded. If not found, the configFilePath will be an empty string.\r\n const configFilePath: string = TSDocConfigFile.findConfigPathForFolder(sourceFileFolder);\r\n\r\n // If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key\r\n // (instead of an empty string)\r\n const cacheKey: string = configFilePath || sourceFileFolder + '/';\r\n Debug.log(`Cache key: \"${cacheKey}\"`);\r\n\r\n const nowMs: number = ConfigCache._getTimeInMs();\r\n\r\n let cachedConfig: ICachedConfig | undefined = undefined;\r\n\r\n // Do we have a cached object?\r\n cachedConfig = ConfigCache._cachedConfigs.get(cacheKey);\r\n\r\n if (cachedConfig) {\r\n Debug.log('Cache hit');\r\n\r\n // Is the cached object still valid?\r\n const loadAgeMs: number = nowMs - cachedConfig.loadTimeMs;\r\n const lastCheckAgeMs: number = nowMs - cachedConfig.lastCheckTimeMs;\r\n\r\n if (loadAgeMs > CACHE_EXPIRE_MS || loadAgeMs < 0) {\r\n Debug.log('Evicting because item is expired');\r\n cachedConfig = undefined;\r\n ConfigCache._cachedConfigs.delete(cacheKey);\r\n } else if (lastCheckAgeMs > CACHE_CHECK_INTERVAL_MS || lastCheckAgeMs < 0) {\r\n Debug.log('Checking for modifications');\r\n cachedConfig.lastCheckTimeMs = nowMs;\r\n if (cachedConfig.configFile.checkForModifiedFiles()) {\r\n // Invalidate the cache because it failed to load completely\r\n Debug.log('Evicting because item was modified');\r\n cachedConfig = undefined;\r\n ConfigCache._cachedConfigs.delete(cacheKey);\r\n }\r\n }\r\n }\r\n\r\n // Load the object\r\n if (!cachedConfig) {\r\n if (ConfigCache._cachedConfigs.size > CACHE_MAX_SIZE) {\r\n Debug.log('Clearing cache');\r\n ConfigCache._cachedConfigs.clear(); // avoid a memory leak\r\n }\r\n\r\n const configFile: TSDocConfigFile = TSDocConfigFile.loadFile(configFilePath);\r\n\r\n if (configFile.fileNotFound) {\r\n Debug.log(`File not found: \"${configFilePath}\"`);\r\n } else {\r\n Debug.log(`Loaded: \"${configFilePath}\"`);\r\n }\r\n\r\n cachedConfig = {\r\n configFile,\r\n lastCheckTimeMs: nowMs,\r\n loadTimeMs: nowMs\r\n };\r\n\r\n ConfigCache._cachedConfigs.set(cacheKey, cachedConfig);\r\n }\r\n\r\n return cachedConfig.configFile;\r\n }\r\n}\r\n"]}
\No newline at end of file