|
| 1 | +import { TSDocConfigFile } from '@microsoft/tsdoc-config'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +import { Debug } from './Debug'; |
| 5 | + |
| 6 | +interface ICachedConfig { |
| 7 | + loadTimeMs: number; |
| 8 | + lastCheckTimeMs: number; |
| 9 | + configFile: TSDocConfigFile; |
| 10 | +} |
| 11 | + |
| 12 | +// How often to check for modified input files. If a file's modification timestamp has changed, then we will |
| 13 | +// evict the cache entry immediately. |
| 14 | +const CACHE_CHECK_INTERVAL_MS: number = 3 * 1000; |
| 15 | + |
| 16 | +// Evict old entries from the cache after this much time, regardless of whether the file was detected as being |
| 17 | +// modified or not. |
| 18 | +const CACHE_EXPIRE_MS: number = 20 * 1000; |
| 19 | + |
| 20 | +// If this many objects accumulate in the cache, then it is cleared to avoid a memory leak. |
| 21 | +const CACHE_MAX_SIZE: number = 100; |
| 22 | + |
| 23 | +export class ConfigCache { |
| 24 | + // findConfigPathForFolder() result --> loaded tsdoc.json configuration |
| 25 | + private static _cachedConfigs: Map<string, ICachedConfig> = new Map<string, ICachedConfig>(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Node.js equivalent of performance.now(). |
| 29 | + */ |
| 30 | + private static _getTimeInMs(): number { |
| 31 | + const [seconds, nanoseconds] = process.hrtime(); |
| 32 | + return seconds * 1000 + nanoseconds / 1000000; |
| 33 | + } |
| 34 | + |
| 35 | + public static getForSourceFile(sourceFilePath: string): TSDocConfigFile { |
| 36 | + const sourceFileFolder: string = path.dirname(path.resolve(sourceFilePath)); |
| 37 | + |
| 38 | + // First, determine the file to be loaded. If not found, the configFilePath will be an empty string. |
| 39 | + const configFilePath: string = TSDocConfigFile.findConfigPathForFolder(sourceFileFolder); |
| 40 | + |
| 41 | + // If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key |
| 42 | + // (instead of an empty string) |
| 43 | + const cacheKey: string = configFilePath || (sourceFileFolder + '/'); |
| 44 | + Debug.log(`Cache key: "${cacheKey}"`); |
| 45 | + |
| 46 | + const nowMs: number = ConfigCache._getTimeInMs(); |
| 47 | + |
| 48 | + let cachedConfig: ICachedConfig | undefined = undefined; |
| 49 | + |
| 50 | + // Do we have a cached object? |
| 51 | + cachedConfig = ConfigCache._cachedConfigs.get(cacheKey); |
| 52 | + |
| 53 | + if (cachedConfig) { |
| 54 | + Debug.log('Cache hit'); |
| 55 | + |
| 56 | + // Is the cached object still valid? |
| 57 | + const loadAgeMs: number = nowMs - cachedConfig.loadTimeMs; |
| 58 | + const lastCheckAgeMs: number = nowMs - cachedConfig.lastCheckTimeMs; |
| 59 | + |
| 60 | + if (loadAgeMs > CACHE_EXPIRE_MS || loadAgeMs < 0) { |
| 61 | + Debug.log('Evicting because item is expired'); |
| 62 | + cachedConfig = undefined; |
| 63 | + ConfigCache._cachedConfigs.delete(cacheKey); |
| 64 | + } else if (lastCheckAgeMs > CACHE_CHECK_INTERVAL_MS || lastCheckAgeMs < 0) { |
| 65 | + Debug.log('Checking for modifications'); |
| 66 | + cachedConfig.lastCheckTimeMs = nowMs; |
| 67 | + if (cachedConfig.configFile.checkForModifiedFiles()) { |
| 68 | + // Invalidate the cache because it failed to load completely |
| 69 | + Debug.log('Evicting because item was modified'); |
| 70 | + cachedConfig = undefined; |
| 71 | + ConfigCache._cachedConfigs.delete(cacheKey); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Load the object |
| 77 | + if (!cachedConfig) { |
| 78 | + if (ConfigCache._cachedConfigs.size > CACHE_MAX_SIZE) { |
| 79 | + Debug.log('Clearing cache'); |
| 80 | + ConfigCache._cachedConfigs.clear(); // avoid a memory leak |
| 81 | + } |
| 82 | + |
| 83 | + const configFile: TSDocConfigFile = TSDocConfigFile.loadFile(configFilePath); |
| 84 | + |
| 85 | + if (configFile.fileNotFound) { |
| 86 | + Debug.log(`File not found: "${configFilePath}"`); |
| 87 | + } else { |
| 88 | + Debug.log(`Loaded: "${configFilePath}"`); |
| 89 | + } |
| 90 | + |
| 91 | + cachedConfig = { |
| 92 | + configFile, |
| 93 | + lastCheckTimeMs: nowMs, |
| 94 | + loadTimeMs: nowMs |
| 95 | + }; |
| 96 | + |
| 97 | + ConfigCache._cachedConfigs.set(cacheKey, cachedConfig); |
| 98 | + } |
| 99 | + |
| 100 | + return cachedConfig.configFile; |
| 101 | + } |
| 102 | +} |
0 commit comments