public static bool HasValidCache(this IRequest req, CacheInfo cacheInfo) { if (cacheInfo == null) { return(false); } var cache = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache; var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None; if (doHttpCaching) { var lastModified = cache.Get <DateTime?>(cacheInfo.LastModifiedKey()); if (req.HasValidCache(lastModified)) { req.Response.EndNotModified(); return(true); } } var encoding = req.GetCompressionType(); var responseBytes = encoding != null ? cache.Get <byte[]>(cacheInfo.CacheKey + "." + encoding) : cache.Get <byte[]>(cacheInfo.CacheKey); if (responseBytes != null) { if (encoding != null) { req.Response.AddHeader(HttpHeaders.ContentEncoding, encoding); } req.Response.WriteBytesToResponse(responseBytes, req.ResponseContentType); return(true); } return(false); }
public static async Task<bool> HandleValidCache(this IRequest req, CacheInfo cacheInfo, CancellationToken token=default) { if (cacheInfo == null) return false; ICacheClient cache; ICacheClientAsync cacheAsync = null; // only non-null if native ICacheClientAsync exists if (cacheInfo.LocalCache) cache = HostContext.AppHost.GetMemoryCacheClient(req); else HostContext.AppHost.TryGetNativeCacheClient(req, out cache, out cacheAsync); var cacheControl = HostContext.GetPlugin<HttpCacheFeature>().BuildCacheControlHeader(cacheInfo); var res = req.Response; DateTime? lastModified = null; var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None; if (doHttpCaching) { lastModified = cacheAsync != null ? await cacheAsync.GetAsync<DateTime?>(cacheInfo.LastModifiedKey(), token).ConfigAwait() : cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); if (req.HasValidCache(lastModified)) { if (cacheControl != null) res.AddHeader(HttpHeaders.CacheControl, cacheControl); res.EndNotModified(); return true; } } var encoding = !cacheInfo.NoCompression ? req.GetCompressionType() : null; var useCacheKey = encoding != null ? cacheInfo.CacheKey + "." + encoding : cacheInfo.CacheKey; var responseBytes = cacheAsync != null ? await cacheAsync.GetAsync<byte[]>(useCacheKey, token).ConfigAwait() : cache.Get<byte[]>(useCacheKey); if (responseBytes != null) { if (encoding != null) res.AddHeader(HttpHeaders.ContentEncoding, encoding); if (cacheInfo.VaryByUser) res.AddHeader(HttpHeaders.Vary, "Cookie"); if (cacheControl != null) res.AddHeader(HttpHeaders.CacheControl, cacheControl); if (!doHttpCaching) { lastModified = cacheAsync != null ? await cacheAsync.GetAsync<DateTime?>(cacheInfo.LastModifiedKey(), token).ConfigAwait() : cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); } if (lastModified != null) res.AddHeader(HttpHeaders.LastModified, lastModified.Value.ToUniversalTime().ToString("r")); await res.WriteBytesToResponse(responseBytes, req.ResponseContentType, token).ConfigAwait(); return true; } return false; }
public static bool HandleValidCache(this IRequest req, CacheInfo cacheInfo) { if (cacheInfo == null) return false; var res = req.Response; var cache = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache; DateTime? lastModified = null; var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None; if (doHttpCaching) { lastModified = cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); if (req.HasValidCache(lastModified)) { res.EndNotModified(); return true; } } var encoding = !cacheInfo.NoCompression ? req.GetCompressionType() : null; var responseBytes = encoding != null ? cache.Get<byte[]>(cacheInfo.CacheKey + "." + encoding) : cache.Get<byte[]>(cacheInfo.CacheKey); if (responseBytes != null) { if (encoding != null) res.AddHeader(HttpHeaders.ContentEncoding, encoding); if (cacheInfo.VaryByUser) res.AddHeader(HttpHeaders.Vary, "Cookie"); var cacheControl = HostContext.GetPlugin<HttpCacheFeature>().BuildCacheControlHeader(cacheInfo); if (cacheControl != null) res.AddHeader(HttpHeaders.CacheControl, cacheControl); if (!doHttpCaching) lastModified = cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); if (lastModified != null) res.AddHeader(HttpHeaders.LastModified, lastModified.Value.ToUniversalTime().ToString("r")); res.WriteBytesToResponse(responseBytes, req.ResponseContentType); return true; } return false; }
public static bool HandleValidCache(this IRequest req, CacheInfo cacheInfo) { if (cacheInfo == null) { return(false); } var res = req.Response; var cache = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache; DateTime?lastModified = null; var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None; if (doHttpCaching) { lastModified = cache.Get <DateTime?>(cacheInfo.LastModifiedKey()); if (req.HasValidCache(lastModified)) { res.EndNotModified(); return(true); } } var encoding = req.GetCompressionType(); var responseBytes = encoding != null ? cache.Get <byte[]>(cacheInfo.CacheKey + "." + encoding) : cache.Get <byte[]>(cacheInfo.CacheKey); if (responseBytes != null) { if (encoding != null) { res.AddHeader(HttpHeaders.ContentEncoding, encoding); } if (cacheInfo.VaryByUser) { res.AddHeader(HttpHeaders.Vary, "Cookie"); } var cacheControl = HostContext.GetPlugin <HttpCacheFeature>().BuildCacheControlHeader(cacheInfo); if (cacheControl != null) { res.AddHeader(HttpHeaders.CacheControl, cacheControl); } if (!doHttpCaching) { lastModified = cache.Get <DateTime?>(cacheInfo.LastModifiedKey()); } if (lastModified != null) { res.AddHeader(HttpHeaders.LastModified, lastModified.Value.ToUniversalTime().ToString("r")); } res.WriteBytesToResponse(responseBytes, req.ResponseContentType); return(true); } return(false); }