private void AddResponseToCache(HttpContext context, OutputCacheProfile profile, OutputCacheResponse response)
        {
            var hostingEnvironment = context.RequestServices.GetRequiredService <IHostingEnvironment>();
            var options            = profile.BuildMemoryCacheEntryOptions(hostingEnvironment);

            _cache.Set(_cacheKeysProvider.GetRequestCacheKey(context, profile), response, options);
        }
        private void AddProfileToCache(HttpContext context, OutputCacheProfile profile)
        {
            var profileCacheEntryOptions = new MemoryCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = _cacheOptions.ProfileCacheDuration
            };

            _cache.Set(_cacheKeysProvider.GetCacheProfileCacheKey(context.Request), profile, profileCacheEntryOptions);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Enabled output caching of the response.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="profile">The caching profile to use.</param>
        public static void EnableOutputCaching(this HttpContext context, OutputCacheProfile profile)
        {
            var    slidingExpiration = TimeSpan.FromSeconds(profile.Duration);
            string varyByHeader      = profile.VaryByHeader;
            string varyByParam       = profile.VaryByParam;

            string[] fileDependencies      = profile.FileDependencies.ToArray();
            bool     useAbsoluteExpiration = profile.UseAbsoluteExpiration;

            context.EnableOutputCaching(slidingExpiration, varyByHeader, varyByParam, useAbsoluteExpiration, fileDependencies);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enabled output caching of the response.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="slidingExpiration">The amount of seconds to cache the output for.</param>
        /// <param name="varyByHeaders">Comma separated list of HTTP headers to vary the caching by.</param>
        /// <param name="varyByParam">Comma separated list of query string parameter names to vary the caching by.</param>
        /// <param name="useAbsoluteExpiration">Use absolute expiration instead of the default sliding expiration.</param>
        /// <param name="fileDependencies">Globbing patterns</param>
        public static void EnableOutputCaching(this HttpContext context, TimeSpan slidingExpiration, string varyByHeaders = null, string varyByParam = null, bool useAbsoluteExpiration = false, params string[] fileDependencies)
        {
            OutputCacheProfile feature = context.Features.Get <OutputCacheProfile>();

            if (feature == null)
            {
                feature = new OutputCacheProfile();
                context.Features.Set(feature);
            }

            feature.Duration              = slidingExpiration.TotalSeconds;
            feature.FileDependencies      = fileDependencies;
            feature.VaryByHeader          = varyByHeaders;
            feature.VaryByParam           = varyByParam;
            feature.UseAbsoluteExpiration = useAbsoluteExpiration;
        }
        public string GetRequestCacheKey(HttpContext context, OutputCacheProfile profile, JObject jObject)
        {
            HttpRequest request = context.Request;
            string      key     = GetCacheProfileCacheKey(request) + "_";

            if (!string.IsNullOrEmpty(profile.VaryByParam))
            {
                key = profile.VaryByParam.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                      .Where(param =>
                             param == "*" || request.Query.ContainsKey(param) || (jObject != null && jObject.ContainsKey(param)))
                      .Aggregate(key,
                                 (current, param) =>
                                 current + (jObject != null
                                ? param + "=" + jObject.SelectToken(param)
                                : param + "=" + request.Query[param]));
            }

            if (!string.IsNullOrEmpty(profile.VaryByHeader))
            {
                key = profile.VaryByHeader.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                      .Where(header => header == "*" || request.Headers.ContainsKey(header)).Aggregate(key,
                                                                                                       (current, header) => current + (header + "=" + request.Headers[header]));
            }

            if (!string.IsNullOrEmpty(profile.VaryByCustom))
            {
                IOutputCacheVaryByCustomService varyByCustomService = context.RequestServices.GetService <IOutputCacheVaryByCustomService>();

                if (varyByCustomService != null)
                {
                    key = profile.VaryByCustom.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(key,
                                                                                                                     (current, argument) =>
                                                                                                                     current + (argument + "=" + varyByCustomService.GetVaryByCustomString(argument)));
                }
            }

            if (profile.IsUserBased && context.User.Identity.IsAuthenticated)
            {
                key += "UserId=" + context.User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier)?.Value;
            }

            return(key);
        }
Exemplo n.º 6
0
        public string GetRequestCacheKey(HttpContext context, OutputCacheProfile profile)
        {
            HttpRequest request = context.Request;
            string      key     = GetCacheProfileCacheKey(request) + "_";

            if (!string.IsNullOrEmpty(profile.VaryByParam))
            {
                foreach (string param in profile.VaryByParam.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (param == "*" || request.Query.ContainsKey(param))
                    {
                        key += param + "=" + request.Query[param];
                    }
                }
            }

            if (!string.IsNullOrEmpty(profile.VaryByHeader))
            {
                foreach (string header in profile.VaryByHeader.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (header == "*" || request.Headers.ContainsKey(header))
                    {
                        key += header + "=" + request.Headers[header];
                    }
                }
            }

            if (!string.IsNullOrEmpty(profile.VaryByCustom))
            {
                var varyByCustomService = context.RequestServices.GetService <IOutputCacheVaryByCustomService>();

                if (varyByCustomService != null)
                {
                    foreach (string argument in profile.VaryByCustom.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        key += argument + "=" + varyByCustomService.GetVaryByCustomString(argument);
                    }
                }
            }

            return(key);
        }
 /// <summary>
 /// Creates a new instance of the entry.
 /// </summary>
 public OutputCacheResponseEntry(HttpContext context, byte[] body, OutputCacheProfile profile)
 {
     _profile = profile;
     AddResponse(context, new OutputCacheResponse(body, context.Response.Headers));
 }
Exemplo n.º 8
0
        internal static bool IsOutputCachingEnabled(this HttpContext context, out OutputCacheProfile profile)
        {
            profile = context.Features.Get <OutputCacheProfile>();

            return(profile != null && profile.Duration > 0);
        }
Exemplo n.º 9
0
 public string GetRequestCacheKey(HttpContext context, OutputCacheProfile profile, JObject jObject)
 {
     return(_cacheKeysProvider.GetRequestCacheKey(context, profile, jObject));
 }