public bool TryGetValue(HttpContext context, out OutputCacheResponse response)
 {
     response = null;
     return
         (_cache.TryGetValue(_cacheKeysProvider.GetCacheProfileCacheKey(context.Request), out OutputCacheProfile profile) &&
          _cache.TryGetValue(_cacheKeysProvider.GetRequestCacheKey(context, profile), out response));
 }
 public void Set(HttpContext context, OutputCacheResponse response)
 {
     if (context.IsOutputCachingEnabled(out OutputCacheProfile profile))
     {
         AddProfileToCache(context, profile);
         AddResponseToCache(context, profile, response);
     }
 }
Beispiel #3
0
        public void Set(HttpContext context, OutputCacheResponse response, JObject jObject)
        {
            if (!context.IsOutputCachingEnabled(out OutputCacheProfile profile))
            {
                return;
            }

            AddProfileToCache(context, profile);
            AddResponseToCache(context, profile, response, jObject);
        }
        private static async Task ServeFromCacheAsync(HttpContext context, OutputCacheResponse value)
        {
            // Copy over the HTTP headers
            foreach (string name in value.Headers.Keys.Where(name => !context.Response.Headers.ContainsKey(name)))
            {
                context.Response.Headers[name] = value.Headers[name];
            }

            // Serve a conditional GET request when if-none-match header exist
            if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out StringValues etag) &&
                context.Response.Headers[HeaderNames.ETag] == etag)
            {
                context.Response.ContentLength = 0;
                context.Response.StatusCode    = StatusCodes.Status304NotModified;
            }
            else
            {
                await context.Response.Body.WriteAsync(value.Body, 0, value.Body.Length);
            }
        }
        internal bool IsCached(HttpContext context, out OutputCacheResponse response)
        {
            string key = GetCacheKey(context);

            return(_responses.TryGetValue(key, out response));
        }
        internal void AddResponse(HttpContext context, OutputCacheResponse response)
        {
            string key = GetCacheKey(context);

            _responses[key] = response;
        }
        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);
        }