Beispiel #1
0
        private ViewLocationCacheResult LocatePageFromPath(string executingFilePath, string pagePath, bool isMainPage)
        {
            var applicationRelativePath = GetAbsolutePath(executingFilePath, pagePath);
            var cacheKey = new ViewLocationCacheKey(applicationRelativePath, isMainPage);

            if (!ViewLookupCache.TryGetValue(cacheKey, out ViewLocationCacheResult cacheResult))
            {
                var expirationTokens = new HashSet <IChangeToken>();
                cacheResult = CreateCacheResult(expirationTokens, applicationRelativePath, isMainPage);

                var cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
                foreach (var expirationToken in expirationTokens)
                {
                    cacheEntryOptions.AddExpirationToken(expirationToken);
                }

                // No views were found at the specified location. Create a not found result.
                if (cacheResult == null)
                {
                    cacheResult = new ViewLocationCacheResult(new[] { applicationRelativePath });
                }

                cacheResult = ViewLookupCache.Set(
                    cacheKey,
                    cacheResult,
                    cacheEntryOptions);
            }

            return(cacheResult);
        }
Beispiel #2
0
    private ViewLocationCacheResult LocatePageFromViewLocations(
        ActionContext actionContext,
        string pageName,
        bool isMainPage)
    {
        var    controllerName = GetNormalizedRouteValue(actionContext, ControllerKey);
        var    areaName       = GetNormalizedRouteValue(actionContext, AreaKey);
        string?razorPageName  = null;

        if (actionContext.ActionDescriptor.RouteValues.ContainsKey(PageKey))
        {
            // Only calculate the Razor Page name if "page" is registered in RouteValues.
            razorPageName = GetNormalizedRouteValue(actionContext, PageKey);
        }

        var expanderContext = new ViewLocationExpanderContext(
            actionContext,
            pageName,
            controllerName,
            areaName,
            razorPageName,
            isMainPage);
        Dictionary <string, string?>?expanderValues = null;

        var expanders = _options.ViewLocationExpanders;
        // Read interface .Count once rather than per iteration
        var expandersCount = expanders.Count;

        if (expandersCount > 0)
        {
            expanderValues         = new Dictionary <string, string?>(StringComparer.Ordinal);
            expanderContext.Values = expanderValues;

            // Perf: Avoid allocations
            for (var i = 0; i < expandersCount; i++)
            {
                expanders[i].PopulateValues(expanderContext);
            }
        }

        var cacheKey = new ViewLocationCacheKey(
            expanderContext.ViewName,
            expanderContext.ControllerName,
            expanderContext.AreaName,
            expanderContext.PageName,
            expanderContext.IsMainPage,
            expanderValues);

        if (!ViewLookupCache.TryGetValue <ViewLocationCacheResult>(cacheKey, out var cacheResult) || cacheResult is null)
        {
            Log.ViewLookupCacheMiss(_logger, cacheKey.ViewName, cacheKey.ControllerName);
            cacheResult = OnCacheMiss(expanderContext, cacheKey);
        }
        else
        {
            Log.ViewLookupCacheHit(_logger, cacheKey.ViewName, cacheKey.ControllerName);
        }

        return(cacheResult);
    }
        private ViewLocationCacheResult LocatePageFromViewLocations(
            ActionContext actionContext,
            string pageName,
            bool isMainPage)
        {
            var    controllerName = GetNormalizedRouteValue(actionContext, ControllerKey);
            var    areaName       = GetNormalizedRouteValue(actionContext, AreaKey);
            string razorPageName  = null;

            if (actionContext.ActionDescriptor.RouteValues.ContainsKey(PageKey))
            {
                // Only calculate the Razor Page name if "page" is registered in RouteValues.
                razorPageName = GetNormalizedRouteValue(actionContext, PageKey);
            }

            var expanderContext = new ViewLocationExpanderContext(
                actionContext,
                pageName,
                controllerName,
                areaName,
                razorPageName,
                isMainPage);
            Dictionary <string, string> expanderValues = null;

            if (_options.ViewLocationExpanders.Count > 0)
            {
                expanderValues         = new Dictionary <string, string>(StringComparer.Ordinal);
                expanderContext.Values = expanderValues;

                // Perf: Avoid allocations
                for (var i = 0; i < _options.ViewLocationExpanders.Count; i++)
                {
                    _options.ViewLocationExpanders[i].PopulateValues(expanderContext);
                }
            }

            var cacheKey = new ViewLocationCacheKey(
                expanderContext.ViewName,
                expanderContext.ControllerName,
                expanderContext.AreaName,
                expanderContext.PageName,
                expanderContext.IsMainPage,
                expanderValues);

            var cacheResult = OnCacheMiss(expanderContext, cacheKey);

            //if (!ViewLookupCache.TryGetValue(cacheKey, out ViewLocationCacheResult cacheResult))
            //   {
            //       _logger.ViewLookupCacheMiss(cacheKey.ViewName, cacheKey.ControllerName);
            //       cacheResult = OnCacheMiss(expanderContext, cacheKey);
            //   }
            //   else
            //   {
            //       _logger.ViewLookupCacheHit(cacheKey.ViewName, cacheKey.ControllerName);
            //   }

            return(cacheResult);
        }
Beispiel #4
0
    private ViewLocationCacheResult OnCacheMiss(
        ViewLocationExpanderContext expanderContext,
        ViewLocationCacheKey cacheKey)
    {
        var viewLocations = GetViewLocationFormats(expanderContext);

        var expanders = _options.ViewLocationExpanders;
        // Read interface .Count once rather than per iteration
        var expandersCount = expanders.Count;

        for (var i = 0; i < expandersCount; i++)
        {
            viewLocations = expanders[i].ExpandViewLocations(expanderContext, viewLocations);
        }

        ViewLocationCacheResult?cacheResult = null;
        var searchedLocations = new List <string>();
        var expirationTokens  = new HashSet <IChangeToken>();

        foreach (var location in viewLocations)
        {
            var path = string.Format(
                CultureInfo.InvariantCulture,
                location,
                expanderContext.ViewName,
                expanderContext.ControllerName,
                expanderContext.AreaName);

            path = ViewEnginePath.ResolvePath(path);

            cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
            if (cacheResult != null)
            {
                break;
            }

            searchedLocations.Add(path);
        }

        // No views were found at the specified location. Create a not found result.
        if (cacheResult == null)
        {
            cacheResult = new ViewLocationCacheResult(searchedLocations);
        }

        var cacheEntryOptions = new MemoryCacheEntryOptions();

        cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
        foreach (var expirationToken in expirationTokens)
        {
            cacheEntryOptions.AddExpirationToken(expirationToken);
        }

        ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions);
        return(cacheResult);
    }
Beispiel #5
0
        private ViewLocationCacheResult OnCacheMiss(
            ViewLocationExpanderContext expanderContext,
            ViewLocationCacheKey cacheKey)
        {
            var sb = new StringBuilder();

            sb.Append(expanderContext.ViewName)
            .Append(expanderContext.ControllerName)
            .Append(expanderContext.AreaName);

            var key = sb.ToString();

            if (!_viewLocations1.ContainsKey(key))
            {
                var viewLocations = GetViewLocationFormats(expanderContext);
                for (var i = 0; i < _options.ViewLocationExpanders.Count; i++)
                {
                    viewLocations = _options.ViewLocationExpanders[i]
                                    .ExpandViewLocations(expanderContext, viewLocations);
                }

                var resolvedLocations = new List <string>();
                foreach (var location in viewLocations)
                {
                    var path = string.Format(
                        CultureInfo.InvariantCulture,
                        location,
                        expanderContext.ViewName,
                        expanderContext.ControllerName,
                        expanderContext.AreaName);

                    path = ViewEnginePath.ResolvePath(path);
                    resolvedLocations.Add(path);
                }

                _viewLocations1[key] = resolvedLocations;
            }

            ViewLocationCacheResult cacheResult = null;
            var searchedLocations = new List <string>();
            var expirationTokens  = new HashSet <IChangeToken>();

            foreach (var path in _viewLocations1[key])
            {
                cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
                if (cacheResult != null)
                {
                    break;
                }

                searchedLocations.Add(path);
            }

            // No views were found at the specified location. Create a not found result.
            if (cacheResult == null)
            {
                cacheResult = new ViewLocationCacheResult(searchedLocations);
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions();

            cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
            //foreach (var expirationToken in expirationTokens)
            //{
            //    cacheEntryOptions.AddExpirationToken(expirationToken);
            //}

            return(ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions));
        }
Beispiel #6
0
        private ViewLocationCacheResult OnCacheMiss(
            ViewLocationExpanderContext expanderContext,
            ViewLocationCacheKey cacheKey)
        {
            // Only use the area view location formats if we have an area token.
            IEnumerable<string> viewLocations = !string.IsNullOrEmpty(expanderContext.AreaName) ?
                _options.AreaViewLocationFormats :
                _options.ViewLocationFormats;

            for (var i = 0; i < _options.ViewLocationExpanders.Count; i++)
            {
                viewLocations = _options.ViewLocationExpanders[i].ExpandViewLocations(expanderContext, viewLocations);
            }

            ViewLocationCacheResult cacheResult = null;
            var searchedLocations = new List<string>();
            var expirationTokens = new HashSet<IChangeToken>();
            foreach (var location in viewLocations)
            {
                var path = string.Format(
                    CultureInfo.InvariantCulture,
                    location,
                    expanderContext.ViewName,
                    expanderContext.ControllerName,
                    expanderContext.AreaName);

                cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
                if (cacheResult != null)
                {
                    break;
                }

                searchedLocations.Add(path);
            }

            // No views were found at the specified location. Create a not found result.
            if (cacheResult == null)
            {
                cacheResult = new ViewLocationCacheResult(searchedLocations);
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions();
            cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
            foreach (var expirationToken in expirationTokens)
            {
                cacheEntryOptions.AddExpirationToken(expirationToken);
            }

            return ViewLookupCache.Set<ViewLocationCacheResult>(cacheKey, cacheResult, cacheEntryOptions);
        }
Beispiel #7
0
        private ViewLocationCacheResult LocatePageFromViewLocations(
            ActionContext actionContext,
            string pageName,
            bool isMainPage)
        {
            var controllerName = GetNormalizedRouteValue(actionContext, ControllerKey);
            var areaName = GetNormalizedRouteValue(actionContext, AreaKey);
            var expanderContext = new ViewLocationExpanderContext(
                actionContext,
                pageName,
                controllerName,
                areaName,
                isMainPage);
            Dictionary<string, string> expanderValues = null;

            if (_options.ViewLocationExpanders.Count > 0)
            {
                expanderValues = new Dictionary<string, string>(StringComparer.Ordinal);
                expanderContext.Values = expanderValues;

                // Perf: Avoid allocations
                for (var i = 0; i < _options.ViewLocationExpanders.Count; i++)
                {
                    _options.ViewLocationExpanders[i].PopulateValues(expanderContext);
                }
            }

            var cacheKey = new ViewLocationCacheKey(
                expanderContext.ViewName,
                expanderContext.ControllerName,
                expanderContext.AreaName,
                expanderContext.IsMainPage,
                expanderValues);

            ViewLocationCacheResult cacheResult;
            if (!ViewLookupCache.TryGetValue(cacheKey, out cacheResult))
            {
                _logger.ViewLookupCacheMiss(cacheKey.ViewName, cacheKey.ControllerName);
                cacheResult = OnCacheMiss(expanderContext, cacheKey);
            }
            else
            {
                _logger.ViewLookupCacheHit(cacheKey.ViewName, cacheKey.ControllerName);
            }

            return cacheResult;
        }
Beispiel #8
0
        private ViewLocationCacheResult LocatePageFromPath(string executingFilePath, string pagePath, bool isMainPage)
        {
            var applicationRelativePath = GetAbsolutePath(executingFilePath, pagePath);
            var cacheKey = new ViewLocationCacheKey(applicationRelativePath, isMainPage);
            ViewLocationCacheResult cacheResult;
            if (!ViewLookupCache.TryGetValue(cacheKey, out cacheResult))
            {
                var expirationTokens = new HashSet<IChangeToken>();
                cacheResult = CreateCacheResult(expirationTokens, applicationRelativePath, isMainPage);

                var cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
                foreach (var expirationToken in expirationTokens)
                {
                    cacheEntryOptions.AddExpirationToken(expirationToken);
                }

                // No views were found at the specified location. Create a not found result.
                if (cacheResult == null)
                {
                    cacheResult = new ViewLocationCacheResult(new[] { applicationRelativePath });
                }

                cacheResult = ViewLookupCache.Set<ViewLocationCacheResult>(
                    cacheKey,
                    cacheResult,
                    cacheEntryOptions);
            }

            return cacheResult;
        }