using System; using System.Collections.Concurrent; using System.Threading.Tasks; using Microsoft.AspNet.FileSystems; namespace Microsoft.AspNet.Mvc.Razor { public class CompilerCache { private readonly ConcurrentDictionary _cache; public CompilerCache() { _cache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); } public async Task GetOrAdd(IFileInfo file, Func> compile) { // Generate a content id string contentId = file.PhysicalPath + '|' + file.LastModified.Ticks; Type compiledType; if (!_cache.TryGetValue(contentId, out compiledType)) { CompilationResult result = await compile(); _cache.TryAdd(contentId, result.CompiledType); return result; } return CompilationResult.Successful(generatedCode: null, type: compiledType); } } }