Example #1
0
        private CompilerCacheResult CreateCacheEntry(
            string normalizedPath,
            Func <RelativeFileInfo, CompilationResult> compile)
        {
            CompilerCacheResult cacheResult;
            var fileInfo = _fileProvider.GetFileInfo(normalizedPath);
            MemoryCacheEntryOptions cacheEntryOptions;
            CompilerCacheResult     cacheResultToCache;

            if (!fileInfo.Exists)
            {
                cacheResultToCache = CompilerCacheResult.FileNotFound;
                cacheResult        = CompilerCacheResult.FileNotFound;

                cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.AddExpirationToken(_fileProvider.Watch(normalizedPath));
            }
            else
            {
                var relativeFileInfo  = new RelativeFileInfo(fileInfo, normalizedPath);
                var compilationResult = compile(relativeFileInfo).EnsureSuccessful();
                cacheEntryOptions = GetMemoryCacheEntryOptions(normalizedPath);

                // By default the CompilationResult returned by IRoslynCompiler is an instance of
                // UncachedCompilationResult. This type has the generated code as a string property and do not want
                // to cache it. We'll instead cache the unwrapped result.
                cacheResultToCache = new CompilerCacheResult(
                    CompilationResult.Successful(compilationResult.CompiledType));
                cacheResult = new CompilerCacheResult(compilationResult);
            }

            _cache.Set(normalizedPath, cacheResultToCache, cacheEntryOptions);
            return(cacheResult);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of <see cref="CompilerCache"/> populated with precompiled views
        /// specified by <paramref name="precompiledViews"/>.
        /// </summary>
        /// <param name="fileProvider"><see cref="IFileProvider"/> used to locate Razor views.</param>
        /// <param name="precompiledViews">A mapping of application relative paths of view to the precompiled view
        /// <see cref="Type"/>s.</param>
        public CompilerCache(
            IFileProvider fileProvider,
            IDictionary <string, Type> precompiledViews)
            : this(fileProvider)
        {
            if (precompiledViews == null)
            {
                throw new ArgumentNullException(nameof(precompiledViews));
            }

            foreach (var item in precompiledViews)
            {
                var cacheEntry = new CompilerCacheResult(new CompilationResult(item.Value));
                _cache.Set(GetNormalizedPath(item.Key), Task.FromResult(cacheEntry));
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of <see cref="CompilerCache"/> populated with precompiled views
        /// specified by <paramref name="precompiledViews"/>.
        /// </summary>
        /// <param name="fileProvider"><see cref="IFileProvider"/> used to locate Razor views.</param>
        /// <param name="precompiledViews">A mapping of application relative paths of view to the precompiled view
        /// <see cref="Type"/>s.</param>
        public CompilerCache(
            IFileProvider fileProvider,
            IDictionary<string, Type> precompiledViews)
            : this(fileProvider)
        {
            if (precompiledViews == null)
            {
                throw new ArgumentNullException(nameof(precompiledViews));
            }

            foreach (var item in precompiledViews)
            {
                var cacheEntry = new CompilerCacheResult(new CompilationResult(item.Value));
                _cache.Set(GetNormalizedPath(item.Key), Task.FromResult(cacheEntry));
            }
        }
Example #4
0
        internal CompilerCache(
            IEnumerable<RazorFileInfoCollection> razorFileInfoCollections,
            IAssemblyLoadContext loadContext,
            IFileProvider fileProvider)
        {
            _fileProvider = fileProvider;
            _cache = new MemoryCache(new MemoryCacheOptions { CompactOnMemoryPressure = false });

            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 CompilerCacheResult(CompilationResult.Successful(viewType));
                    var normalizedPath = NormalizePath(fileInfo.RelativePath);
                    _cache.Set(normalizedPath, cacheEntry);
                }
            }
        }
Example #5
0
        /// <inheritdoc />
        public CompilerCacheResult GetOrAdd(
            [NotNull] string relativePath,
            [NotNull] Func <RelativeFileInfo, CompilationResult> compile)
        {
            var normalizedPath = NormalizePath(relativePath);
            CompilerCacheResult cacheResult;

            if (!_cache.TryGetValue(normalizedPath, out cacheResult))
            {
                var fileInfo = _fileProvider.GetFileInfo(relativePath);
                MemoryCacheEntryOptions cacheEntryOptions;
                CompilerCacheResult     cacheResultToCache;
                if (!fileInfo.Exists)
                {
                    cacheResultToCache = CompilerCacheResult.FileNotFound;
                    cacheResult        = CompilerCacheResult.FileNotFound;

                    cacheEntryOptions = new MemoryCacheEntryOptions();
                    cacheEntryOptions.AddExpirationTrigger(_fileProvider.Watch(relativePath));
                }
                else
                {
                    var relativeFileInfo  = new RelativeFileInfo(fileInfo, relativePath);
                    var compilationResult = compile(relativeFileInfo).EnsureSuccessful();
                    cacheEntryOptions = GetMemoryCacheEntryOptions(relativePath);

                    // By default the CompilationResult returned by IRoslynCompiler is an instance of
                    // UncachedCompilationResult. This type has the generated code as a string property and do not want
                    // to cache it. We'll instead cache the unwrapped result.
                    cacheResultToCache = new CompilerCacheResult(
                        CompilationResult.Successful(compilationResult.CompiledType));
                    cacheResult = new CompilerCacheResult(compilationResult);
                }

                _cache.Set(normalizedPath, cacheResultToCache, cacheEntryOptions);
            }

            return(cacheResult);
        }
Example #6
0
        internal CompilerCache(
            IEnumerable <RazorFileInfoCollection> razorFileInfoCollections,
            IAssemblyLoadContext loadContext,
            IFileProvider fileProvider)
        {
            _fileProvider = fileProvider;
            _cache        = new MemoryCache(new MemoryCacheOptions {
                CompactOnMemoryPressure = false
            });

            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 CompilerCacheResult(CompilationResult.Successful(viewType));
                    var normalizedPath = NormalizePath(fileInfo.RelativePath);
                    _cache.Set(normalizedPath, cacheEntry);
                }
            }
        }
Example #7
0
        private CompilerCacheResult CreateCacheEntry(
            string normalizedPath,
            Func<RelativeFileInfo, CompilationResult> compile)
        {
            CompilerCacheResult cacheResult;
            var fileInfo = _fileProvider.GetFileInfo(normalizedPath);
            MemoryCacheEntryOptions cacheEntryOptions;
            CompilerCacheResult cacheResultToCache;
            if (!fileInfo.Exists)
            {
                cacheResultToCache = CompilerCacheResult.FileNotFound;
                cacheResult = CompilerCacheResult.FileNotFound;

                cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.AddExpirationToken(_fileProvider.Watch(normalizedPath));
            }
            else
            {
                var relativeFileInfo = new RelativeFileInfo(fileInfo, normalizedPath);
                var compilationResult = compile(relativeFileInfo).EnsureSuccessful();
                cacheEntryOptions = GetMemoryCacheEntryOptions(normalizedPath);

                // By default the CompilationResult returned by IRoslynCompiler is an instance of
                // UncachedCompilationResult. This type has the generated code as a string property and do not want
                // to cache it. We'll instead cache the unwrapped result.
                cacheResultToCache = new CompilerCacheResult(
                    CompilationResult.Successful(compilationResult.CompiledType));
                cacheResult = new CompilerCacheResult(compilationResult);
            }

            _cache.Set(normalizedPath, cacheResultToCache, cacheEntryOptions);
            return cacheResult;
        }
Example #8
0
        /// <inheritdoc />
        public CompilerCacheResult GetOrAdd(
            [NotNull] string relativePath,
            [NotNull] Func<RelativeFileInfo, CompilationResult> compile)
        {
            var normalizedPath = NormalizePath(relativePath);
            CompilerCacheResult cacheResult;
            if (!_cache.TryGetValue(normalizedPath, out cacheResult))
            {
                var fileInfo = _fileProvider.GetFileInfo(relativePath);
                MemoryCacheEntryOptions cacheEntryOptions;
                CompilerCacheResult cacheResultToCache;
                if (!fileInfo.Exists)
                {
                    cacheResultToCache = CompilerCacheResult.FileNotFound;
                    cacheResult = CompilerCacheResult.FileNotFound;

                    cacheEntryOptions = new MemoryCacheEntryOptions();
                    cacheEntryOptions.AddExpirationTrigger(_fileProvider.Watch(relativePath));
                }
                else
                {
                    var relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath);
                    var compilationResult = compile(relativeFileInfo).EnsureSuccessful();
                    cacheEntryOptions = GetMemoryCacheEntryOptions(relativePath);

                    // By default the CompilationResult returned by IRoslynCompiler is an instance of
                    // UncachedCompilationResult. This type has the generated code as a string property and do not want
                    // to cache it. We'll instead cache the unwrapped result.
                    cacheResultToCache = new CompilerCacheResult(
                        CompilationResult.Successful(compilationResult.CompiledType));
                    cacheResult = new CompilerCacheResult(compilationResult);
                }

                _cache.Set(normalizedPath, cacheResultToCache, cacheEntryOptions);
            }

            return cacheResult;
        }