예제 #1
0
        public override void OnActionExecuting(HttpActionContext context)
        {
            bool bFromCache = false;

            if (context != null)
            {
                if (this.IsCacheable(context))
                {
                    _bodyCacheKey        = string.Join(":", context.Request.RequestUri.PathAndQuery, context.Request.Headers.Accept.FirstOrDefault().ToString());
                    _contentTypeCacheKey = string.Concat(_bodyCacheKey, ":response-ct");

                    if (WebApiCache.Contains(_bodyCacheKey))
                    {
                        var data = (string)WebApiCache.Get(_bodyCacheKey);
                        if (data != null)
                        {
                            bFromCache = true;

                            context.Response         = context.Request.CreateResponse();
                            context.Response.Content = new StringContent(data);

                            var contentType = (MediaTypeHeaderValue)WebApiCache.Get(_contentTypeCacheKey);
                            if (contentType == null)
                            {
                                contentType = new MediaTypeHeaderValue(_bodyCacheKey.Split(':')[1]);
                            }

                            context.Response.Content.Headers.ContentType = contentType;
                            context.Response.Headers.CacheControl        = this.SetClientCache();
                        }
                    }
                }
            }

            LoggerServiceHelper.Current.WriteLine(this, LogStatusEnum.Debug, "[{0}] {1} (-> {2})",
                                                  context.GetClientIp(), context.Request.RequestUri.PathAndQuery, bFromCache ? "cache" : "database");
        }