public void Set(string requestCacheKey, OutputCacheResponseEntry entry, HttpContext context) { if (!context.IsOutputCachingEnabled(out OutputCacheProfile profile)) { return; } var env = (IHostingEnvironment)context.RequestServices.GetService(typeof(IHostingEnvironment)); var options = new MemoryCacheEntryOptions(); if (profile.UseAbsoluteExpiration) { options.SetAbsoluteExpiration(TimeSpan.FromSeconds(profile.Duration)); } else { options.SetSlidingExpiration(TimeSpan.FromSeconds(profile.Duration)); } foreach (string globs in profile.FileDependencies) { options.AddExpirationToken(env.ContentRootFileProvider.Watch(globs)); } _cache.Set(requestCacheKey, entry, options); }
private async Task ServeFromMvcAndCacheAsync(HttpContext context, OutputCacheResponseEntry entry) { HttpResponse response = context.Response; Stream originalStream = response.Body; try { using (var ms = new MemoryStream()) { response.Body = ms; await _next(context); if (_options.DoesResponseQualify(context)) { byte[] bytes = ms.ToArray(); AddEtagToResponse(context, bytes); AddResponseToCache(context, entry, bytes); } if (ms.Length > 0) { ms.Seek(0, SeekOrigin.Begin); await ms.CopyToAsync(originalStream); } } } finally { response.Body = originalStream; } }
private void AddResponseToCache(HttpContext context, OutputCacheResponseEntry entry, byte[] bytes) { if (!context.IsOutputCachingEnabled(out OutputCacheProfile profile)) { return; } if (entry == null) { entry = new OutputCacheResponseEntry(context, bytes, profile); _cache.Set(context.Request.Host + context.Request.Path, entry, context); } else { entry.AddResponse(context, new OutputCacheResponse(bytes, context.Response.Headers)); } }
public bool TryGetValue(string requestCacheKey, out OutputCacheResponseEntry value) { return(_cache.TryGetValue(requestCacheKey, out value)); }
public bool TryGetValue(HttpRequest request, out OutputCacheResponseEntry value) { return(TryGetValue(BuildRequestCacheKey(request), out value)); }
public bool TryGetValue(string route, out OutputCacheResponseEntry value) { string cleanRoute = NormalizeRoute(route); return(_cache.TryGetValue(cleanRoute, out value)); }