}// end OnActionExecutedAsync() public override void OnActionExecuting(HttpActionContext actionContext) { log4net.LogManager.GetLogger(this.GetType()).Debug("start: OnActionExecuting"); if (actionContext == null) { throw new ArgumentNullException("actionContext"); } if (!IsCachingAllowed(actionContext, AnonymousOnly)) { return; } var timer = (Stopwatch)actionContext.Request.Properties["logtimer"]; if (timer == null) { timer = new Stopwatch(); timer.Start(); } var config = actionContext.Request.GetConfiguration(); EnsureCacheTimeQuery(); EnsureCache(config, actionContext.Request); var cacheKeyGenerator = config.CacheOutputConfiguration().GetCacheKeyGenerator(actionContext.Request, typeof(CustomCacheKeyGenerator)); if (cacheKeyGenerator is CustomCacheKeyGenerator) { ((CustomCacheKeyGenerator)cacheKeyGenerator).CachePerUser = CachePerUser; ((CustomCacheKeyGenerator)cacheKeyGenerator).ApiName = ApiName; ((CustomCacheKeyGenerator)cacheKeyGenerator).HeadersToInlcudeInCacheKey = HeadersToInlcudeInCacheKey; } var responseMediaType = GetExpectedMediaType(config, actionContext); actionContext.Request.Properties[CurrentRequestMediaType] = responseMediaType; var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, responseMediaType, ExcludeQueryStringFromCacheKey); if (!_webApiCache.Contains(cachekey)) { return; } if (actionContext.Request.Headers.IfNoneMatch != null) { var etag = _webApiCache.Get <string>(cachekey + Constants.EtagKey); if (etag != null) { if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag)) { var time = CacheTimeQuery.Execute(DateTime.UtcNow); var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified); ApplyCacheHeaders(quickResponse, time); actionContext.Response = quickResponse; // Log Api Access if (LogEnabled && !(bool)actionContext.Request.Properties["requestIsLogged"]) { timer.Stop(); APILogger?.LogExposedAPIAccess(_id, actionContext, timer.Elapsed, true); actionContext.Request.Properties["requestIsLogged"] = true; } return; } } } var val = _webApiCache.Get <byte[]>(cachekey); if (val == null) { return; } var contenttype = _webApiCache.Get <MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? responseMediaType; actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new ByteArrayContent(val); actionContext.Response.Content.Headers.ContentType = contenttype; var responseEtag = _webApiCache.Get <string>(cachekey + Constants.EtagKey); if (responseEtag != null) { SetEtag(actionContext.Response, responseEtag); } var cacheTime = CacheTimeQuery.Execute(DateTime.UtcNow); ApplyCacheHeaders(actionContext.Response, cacheTime); log4net.LogManager.GetLogger(this.GetType()).Debug("end: OnActionExecuting"); // Log Api Access if (LogEnabled && !(bool)actionContext.Request.Properties["requestIsLogged"]) { timer.Stop(); APILogger?.LogExposedAPIAccess(_id, actionContext, timer.Elapsed, true); actionContext.Request.Properties["requestIsLogged"] = true; } }
private void OnActionExecuting(HttpActionContext actionContext) { if (actionContext == null) { throw new ArgumentNullException("actionContext"); } if (!_isCachingAllowed(actionContext, AnonymousOnly)) { return; } var config = actionContext.Request.GetConfiguration(); EnsureCacheTimeQuery(); EnsureCache(config, actionContext.Request); var cacheKeyGenerator = config.CacheOutputConfiguration().GetCacheKeyGenerator(actionContext.Request, CacheKeyGenerator); _responseMediaType = GetExpectedMediaType(config, actionContext); var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, _responseMediaType, ExcludeQueryStringFromCacheKey); if (!_webApiCache.Contains(cachekey)) { return; } if (actionContext.Request.Headers.IfNoneMatch != null) { var etag = _webApiCache.Get(cachekey + Constants.EtagKey) as string; if (etag != null) { if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag)) { var time = CacheTimeQuery.Execute(DateTime.Now); var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified); ApplyCacheHeaders(quickResponse, time); actionContext.Response = quickResponse; return; } } } var val = _webApiCache.Get(cachekey) as byte[]; if (val == null) { return; } var contenttype = _webApiCache.Get(cachekey + Constants.ContentTypeKey) as MediaTypeHeaderValue ?? new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1]); actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new ByteArrayContent(val); actionContext.Response.Content.Headers.ContentType = contenttype; var responseEtag = _webApiCache.Get(cachekey + Constants.EtagKey) as string; if (responseEtag != null) { SetEtag(actionContext.Response, responseEtag); } var cacheTime = CacheTimeQuery.Execute(DateTime.Now); ApplyCacheHeaders(actionContext.Response, cacheTime); }
public async override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext == null) { throw new ArgumentNullException("actionContext"); } ProfileElement webApiProfileElement = null; webApiProfileElement = getWebApiCacheProfile(); if (webApiProfileElement != null && webApiProfileElement.Enable) { this.ClientTimeSpan = webApiProfileElement.ClientTimeSpan; this.ServerTimeSpan = webApiProfileElement.ServerTimeSpan; this.MustRevalidate = webApiProfileElement.MustRevalidate; this.ExcludeQueryStringFromCacheKey = webApiProfileElement.ExcludeQueryStringFromCacheKey; this.AnonymousOnly = webApiProfileElement.AnonymousOnly; } //是否允许缓存 if (!IsCachingAllowed(actionContext, AnonymousOnly)) { return; } var config = actionContext.Request.GetConfiguration(); EnsureCacheTimeQuery(); EnsureCache(config, actionContext.Request); //Key var cacheKeyGenerator = config.CacheOutputConfiguration().GetCacheKeyGenerator(actionContext.Request, CacheKeyGenerator); //返回值类型 var responseMediaType = GetExpectedMediaType(config, actionContext); actionContext.Request.Properties[CurrentRequestMediaType] = responseMediaType; //生成Key var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, responseMediaType, ExcludeQueryStringFromCacheKey); if (!_webApiCache.Contains(cachekey)) { return; } //ETag判断 if (actionContext.Request.Headers.IfNoneMatch != null) { var etag = _webApiCache.Get <string>(cachekey + Constants.EtagKey); if (etag != null) { if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag)) { var time = CacheTimeQuery.Execute(DateTime.Now); var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified); ApplyCacheHeaders(quickResponse, time); //输出 actionContext.Response = quickResponse; return; } } } //直接从缓存返回 var val = _webApiCache.Get <byte[]>(cachekey); if (val == null) { return; } var contenttype = _webApiCache.Get <MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]); actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new ByteArrayContent(val); actionContext.Response.Content.Headers.ContentType = contenttype; var responseEtag = _webApiCache.Get <string>(cachekey + Constants.EtagKey); if (responseEtag != null) { SetEtag(actionContext.Response, responseEtag); } var cacheTime = CacheTimeQuery.Execute(DateTime.Now); ApplyCacheHeaders(actionContext.Response, cacheTime); //判断是否需要预读 if (PreloadAllowed(actionContext)) { await PreloadNextPageByHttpClient(actionContext); } }