Beispiel #1
0
        private bool AssociatedViewStartsChanged(CompilerCacheEntry entry,
                                                 Func <RelativeFileInfo, CompilationResult> compile)
        {
            var viewStartEntry = GetCompositeViewStartEntry(entry.RelativePath, compile);

            return(entry.AssociatedViewStartEntry != viewStartEntry);
        }
Beispiel #2
0
        // Internal for unit testing
        internal CompilerCache(IEnumerable <RazorFileInfoCollection> viewCollections, IFileProvider fileProvider)
        {
            _fileProvider = fileProvider;
            _cache        = new ConcurrentDictionary <string, CompilerCacheEntry>(StringComparer.OrdinalIgnoreCase);

            foreach (var viewCollection in viewCollections)
            {
                foreach (var fileInfo in viewCollection.FileInfos)
                {
                    var containingAssembly = viewCollection.GetType().GetTypeInfo().Assembly;
                    var viewType           = containingAssembly.GetType(fileInfo.FullTypeName);
                    var cacheEntry         = new CompilerCacheEntry(fileInfo, viewType);

                    // There shouldn't be any duplicates and if there are any the first will win.
                    // If the result doesn't match the one on disk its going to recompile anyways.
                    _cache.TryAdd(NormalizePath(fileInfo.RelativePath), cacheEntry);
                }
            }

            // Set up ViewStarts
            foreach (var entry in _cache)
            {
                var viewStartLocations = ViewStartUtility.GetViewStartLocations(entry.Key);
                foreach (var location in viewStartLocations)
                {
                    CompilerCacheEntry viewStartEntry;
                    if (_cache.TryGetValue(location, out viewStartEntry))
                    {
                        // Add the the composite _ViewStart entry as a dependency.
                        entry.Value.AssociatedViewStartEntry = viewStartEntry;
                        break;
                    }
                }
            }
        }
Beispiel #3
0
        private bool AssociatedGlobalFilesChanged(CompilerCacheEntry entry,
                                                  Func <RelativeFileInfo, CompilationResult> compile)
        {
            var globalFileEntry = GetCompositeGlobalFileEntry(entry.RelativePath, compile);

            return(entry.AssociatedGlobalFileEntry != globalFileEntry);
        }
Beispiel #4
0
        private CompilationResult OnCacheMiss(RelativeFileInfo file,
                                              bool isInstrumented,
                                              Func <CompilationResult> compile)
        {
            var result = compile();

            var cacheEntry = new CompilerCacheEntry(file, result.CompiledType, isInstrumented);

            _cache[NormalizePath(file.RelativePath)] = cacheEntry;

            return(result);
        }
Beispiel #5
0
        internal CompilerCache(IEnumerable <RazorFileInfoCollection> viewCollections) : this()
        {
            foreach (var viewCollection in viewCollections)
            {
                foreach (var fileInfo in viewCollection.FileInfos)
                {
                    var containingAssembly = viewCollection.GetType().GetTypeInfo().Assembly;
                    var viewType           = containingAssembly.GetType(fileInfo.FullTypeName);
                    var cacheEntry         = new CompilerCacheEntry(fileInfo, viewType);

                    // There shouldn't be any duplicates and if there are any the first will win.
                    // If the result doesn't match the one on disk its going to recompile anyways.
                    _cache.TryAdd(NormalizePath(fileInfo.RelativePath), cacheEntry);
                }
            }
        }
Beispiel #6
0
        private CompilerCacheEntry OnCacheMiss(RelativeFileInfo file,
                                               string normalizedPath,
                                               Func <RelativeFileInfo, CompilationResult> compile,
                                               out CompilationResult result)
        {
            result = compile(file);

            var cacheEntry = new CompilerCacheEntry(file, result.CompiledType)
            {
                AssociatedViewStartEntry = GetCompositeViewStartEntry(normalizedPath, compile)
            };

            // The cache is a concurrent dictionary, so concurrent addition to it with the same key would result in a
            // safe race.
            _cache[normalizedPath] = cacheEntry;
            return(cacheEntry);
        }
Beispiel #7
0
        internal CompilerCache(IEnumerable <RazorFileInfoCollection> razorFileInfoCollections,
                               IAssemblyLoadContext loadContext,
                               IFileProvider fileProvider)
        {
            _fileProvider = fileProvider;
            _cache        = new MemoryCache(new MemoryCacheOptions {
                ListenForMemoryPressure = false
            });

            var cacheEntries = new List <CompilerCacheEntry>();

            foreach (var viewCollection in razorFileInfoCollections)
            {
                var containingAssembly = viewCollection.LoadAssembly(loadContext);
                foreach (var fileInfo in viewCollection.FileInfos)
                {
                    var viewType   = containingAssembly.GetType(fileInfo.FullTypeName);
                    var cacheEntry = new CompilerCacheEntry(fileInfo, viewType);

                    // There shouldn't be any duplicates and if there are any the first will win.
                    // If the result doesn't match the one on disk its going to recompile anyways.
                    _cache.Set(NormalizePath(fileInfo.RelativePath), cacheEntry, PopulateCacheSetContext);

                    cacheEntries.Add(cacheEntry);
                }
            }

            // Set up _GlobalImports
            foreach (var entry in cacheEntries)
            {
                var globalFileLocations = ViewHierarchyUtility.GetGlobalImportLocations(entry.RelativePath);
                foreach (var location in globalFileLocations)
                {
                    var globalFileEntry = _cache.Get <CompilerCacheEntry>(location);
                    if (globalFileEntry != null)
                    {
                        // Add the the composite _GlobalImport entry as a dependency.
                        entry.AssociatedGlobalFileEntry = globalFileEntry;
                        break;
                    }
                }
            }
        }