Beispiel #1
0
        public override void OnActionExecuted(HttpActionExecutedContext filterContext)
        {
            if (TimeToLiveLevel == TimeToLiveLevel.None)
            {
                var response      = filterContext.Response;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);


                #region - Downstream Cacheability

                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    Public       = false,
                    NoTransform  = true,
                    MaxAge       = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs),
                    SharedMaxAge = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs)
                };

                response.Headers.Add("Vary", "Accept-Encoding");

                #endregion


                #region - Edge Cacheability

                response.Headers.Add("Edge-Control",
                                     String.Format("downstream-ttl={0}{1}, cache-maxage={2}{3}",
                                                   downstreamTtl.Value, downstreamTtl.ShortUnitName, edgeTtl.Value, edgeTtl.ShortUnitName));

                #endregion
            }
            else if (TimeToLiveLevel == TimeToLiveLevel.ZeroRevalidate)
            {
                var response      = filterContext.Response;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);


                #region - Downstream Cacheability

                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    Public          = true,
                    MustRevalidate  = true,
                    ProxyRevalidate = true,
                    NoTransform     = true,
                    MaxAge          = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs),
                    SharedMaxAge    = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs)
                };

                response.Headers.Add("Vary", "Accept-Encoding");

                #endregion


                #region - Edge Cacheability

                response.Headers.Add("Edge-Control",
                                     String.Format("downstream-ttl={0}{1}, cache-maxage={2}{3}",
                                                   downstreamTtl.Value, downstreamTtl.ShortUnitName, edgeTtl.Value, edgeTtl.ShortUnitName));

                #endregion
            }
            else
            {
                var response      = filterContext.Response;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);
                var now           = DateTime.Now;


                #region - Downstream Cacheability

                response.Content.Headers.LastModified = new DateTimeOffset(now);
                response.Content.Headers.Expires      = new DateTimeOffset(now.AddSeconds(downstreamTtl.ValueInSecs));
                response.Headers.CacheControl         = new CacheControlHeaderValue
                {
                    Public       = true,
                    NoTransform  = true,
                    MaxAge       = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs),
                    SharedMaxAge = TimeSpan.FromSeconds(downstreamTtl.ValueInSecs)
                };

                response.Headers.Add("Vary", "Accept-Encoding");

                #endregion


                #region - Edge Cacheability

                response.Headers.Add("Edge-Control",
                                     String.Format("downstream-ttl={0}{1}, cache-maxage={2}{3}",
                                                   downstreamTtl.Value, downstreamTtl.ShortUnitName, edgeTtl.Value, edgeTtl.ShortUnitName));

                #endregion
            }
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (TimeToLiveLevel == TimeToLiveLevel.None)
            {
                var response      = filterContext.HttpContext.Response;
                var cache         = response.Cache;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);


                #region - Downstream Cacheability

                cache.SetCacheability(HttpCacheability.Private);
                cache.SetNoTransforms();

                cache.SetMaxAge(TimeSpan.FromSeconds(downstreamTtl.ValueInSecs));

                #endregion


                #region - Edge Cacheability

                response.AddHeader("Edge-Control",
                                   String.Format("downstream-ttl={0}{1}, cache-maxage={2}{3}",
                                                 downstreamTtl.Value, downstreamTtl.ShortUnitName, edgeTtl.Value, edgeTtl.ShortUnitName));

                #endregion
            }
            else if (TimeToLiveLevel == TimeToLiveLevel.ZeroRevalidate)
            {
                var response      = filterContext.HttpContext.Response;
                var cache         = response.Cache;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);
                var now           = DateTime.Now;


                #region - Downstream Cacheability

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetNoTransforms();
                cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

                cache.SetMaxAge(TimeSpan.FromSeconds(downstreamTtl.ValueInSecs));
                cache.AppendCacheExtension("s-maxage=" + TimeSpan.FromSeconds(downstreamTtl.ValueInSecs).TotalSeconds);
                cache.SetExpires(now.AddSeconds(downstreamTtl.ValueInSecs));
                cache.SetLastModified(now);

                #endregion


                #region - Edge Cacheability

                response.AddHeader("Edge-Control",
                                   String.Format("downstream-ttl={0}{1}, cache-maxage={2}{3}",
                                                 downstreamTtl.Value, downstreamTtl.ShortUnitName, edgeTtl.Value, edgeTtl.ShortUnitName));

                #endregion
            }
            else // this handles all the level-config-string related settings
            {
                var response      = filterContext.HttpContext.Response;
                var cache         = response.Cache;
                var downstreamTtl = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForDownstream, TimeToLiveLevel);
                var edgeTtl       = CacheConfig.ResolveTimeToLiveLevel(TimeToLivesForEdge, TimeToLiveLevel);
                var now           = DateTime.Now;


                #region - Downstream Cacheability

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetNoTransforms();

                cache.SetMaxAge(TimeSpan.FromSeconds(downstreamTtl.ValueInSecs));
                cache.AppendCacheExtension("s-maxage=" + TimeSpan.FromSeconds(downstreamTtl.ValueInSecs).TotalSeconds);
                cache.SetExpires(now.AddSeconds(downstreamTtl.ValueInSecs));
                cache.SetLastModified(now);

                #endregion


                #region - Edge Cacheability

                response.AddHeader("Edge-Control",
                                   $"downstream-ttl={downstreamTtl.Value}{downstreamTtl.ShortUnitName}, cache-maxage={edgeTtl.Value}{edgeTtl.ShortUnitName}");

                #endregion
            }
        }