Exemplo n.º 1
0
        private MemoryCacheEntryOptions GetMemoryCacheEntryOptions(
            RelativeFileInfo fileInfo,
            PrecompilationCacheEntry cacheEntry)
        {
            var options = new MemoryCacheEntryOptions();

            options.AddExpirationTrigger(FileProvider.Watch(fileInfo.RelativePath));
            foreach (var path in ViewHierarchyUtility.GetViewImportsLocations(fileInfo.RelativePath))
            {
                options.AddExpirationTrigger(FileProvider.Watch(path));
            }
            return(options);
        }
Exemplo n.º 2
0
        private MemoryCacheEntryOptions GetMemoryCacheEntryOptions(string relativePath)
        {
            var options = new MemoryCacheEntryOptions();

            options.AddExpirationTrigger(_fileProvider.Watch(relativePath));

            var viewImportsPaths = ViewHierarchyUtility.GetViewImportsLocations(relativePath);

            foreach (var location in viewImportsPaths)
            {
                options.AddExpirationTrigger(_fileProvider.Watch(location));
            }
            return(options);
        }
Exemplo n.º 3
0
        private static IMemoryCache MakeCache(object result = null)
        {
            var cache = new Mock <IMemoryCache>();

            cache.CallBase = true;
            cache.Setup(c => c.TryGetValue(It.IsAny <string>(), out result))
            .Returns(result != null);

            var cacheEntryOptions = new MemoryCacheEntryOptions();

            cacheEntryOptions.AddExpirationTrigger(new Mock <IExpirationTrigger>().Object);
            cache
            .Setup(
                c => c.Set(
                    /*key*/ It.IsAny <string>(),
                    /*value*/ It.IsAny <object>(),
                    /*options*/ cacheEntryOptions))
            .Returns(result);
            return(cache.Object);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 6
0
        private IEnumerable <string> ExpandGlobbedUrl(string include, string exclude)
        {
            if (string.IsNullOrEmpty(include))
            {
                return(Enumerable.Empty <string>());
            }

            var includePatterns = include.Split(PatternSeparator, StringSplitOptions.RemoveEmptyEntries);
            var excludePatterns = exclude?.Split(PatternSeparator, StringSplitOptions.RemoveEmptyEntries);

            if (includePatterns.Length == 0)
            {
                return(Enumerable.Empty <string>());
            }

            if (Cache != null)
            {
                var cacheKey = $"{nameof(GlobbingUrlBuilder)}-inc:{include}-exc:{exclude}";
                IEnumerable <string> files;
                if (!Cache.TryGetValue(cacheKey, out files))
                {
                    var options = new MemoryCacheEntryOptions();
                    foreach (var pattern in includePatterns)
                    {
                        var trigger = FileProvider.Watch(pattern);
                        options.AddExpirationTrigger(trigger);
                    }

                    files = FindFiles(includePatterns, excludePatterns);

                    Cache.Set(cacheKey, files, options);
                }
                return(files);
            }

            return(FindFiles(includePatterns, excludePatterns));
        }
Exemplo n.º 7
0
        private MemoryCacheEntryOptions GetMemoryCacheEntryOptions(string relativePath)
        {
            var options = new MemoryCacheEntryOptions();
            options.AddExpirationTrigger(_fileProvider.Watch(relativePath));

            var viewImportsPaths = ViewHierarchyUtility.GetViewImportsLocations(relativePath);
            foreach (var location in viewImportsPaths)
            {
                options.AddExpirationTrigger(_fileProvider.Watch(location));
            }
            return options;
        }
Exemplo n.º 8
0
        private static IMemoryCache MakeCache(object result = null)
        {
            var cache = new Mock<IMemoryCache>();
            cache.CallBase = true;
            cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out result))
                .Returns(result != null);

            var cacheEntryOptions = new MemoryCacheEntryOptions();
            cacheEntryOptions.AddExpirationTrigger(new Mock<IExpirationTrigger>().Object);
            cache
                .Setup(
                    c => c.Set(
                        /*key*/ It.IsAny<string>(),
                        /*value*/ It.IsAny<object>(),
                        /*options*/ cacheEntryOptions))
                .Returns(result);
            return cache.Object;
        }