Ejemplo n.º 1
0
        public IEnumerable <EdgeCacheRule> EnumerateRules()
        {
            EdgeCacheSection section = GetSection();

            List <EdgeCacheRule> rules = new List <EdgeCacheRule>();

            if (section != null)
            {
                foreach (EdgeCacheRuleElement ruleElement in section.Rules)
                {
                    EdgeCacheRule rule = new EdgeCacheRule(ruleElement.Name, ruleElement.Duration, ruleElement.Debug);

                    foreach (EdgeFilterElement filterElement in ruleElement.Filters)
                    {
                        rule.AddFilter(InstantiateFromTypeName <IEdgeFilter>(filterElement.TypeName));
                    }

                    foreach (EdgeIdentifierElement identifierElement in ruleElement.Identifiers)
                    {
                        rule.AddIdentifier(InstantiateFromTypeName <IEdgeIdentifier>(identifierElement.TypeName));
                    }

                    foreach (EdgeStoreElement storeElement in ruleElement.Stores)
                    {
                        rule.AddStore(InstantiateFromTypeName <IEdgeStore>(storeElement.TypeName));
                    }

                    rules.Add(rule);
                }
            }

            return(rules);
        }
Ejemplo n.º 2
0
        void IEdgeStore.Initialize(EdgeCacheRule rule)
        {
            UpdateCacheDependency(rule);

            rule.ResolveRequestCache += OnResolveRequestCache;

            rule.UpdateRequestCache += OnUpdateRequestCache;
        }
Ejemplo n.º 3
0
        public bool Include(EdgeCacheRule rule, OriginUrl url)
        {
            RegexEdgeFilterGroup group;

            if (_groups.TryGetValue(rule.Name, out group))
            {
                return(group.Include(url));
            }

            return(false);
        }
Ejemplo n.º 4
0
        private void OnPreSendRequestHeaders(object sender, EdgeCacheEventArgs e)
        {
            EdgeCacheRule rule = (EdgeCacheRule)sender;

            HttpContext context = e.Context;

            int minutes = rule.Duration;

            HttpResponse response = context.Response;

            HttpCachePolicy policy = response.Cache;

            policy.SetMaxAge(TimeSpan.FromMinutes(minutes));
            policy.SetCacheability(HttpCacheability.ServerAndPrivate);
            policy.SetSlidingExpiration(true);
        }
Ejemplo n.º 5
0
        private void OnUpdateRequestCache(object sender, EdgeCacheEventArgs e)
        {
            EdgeCacheRule rule = (EdgeCacheRule)sender;

            HttpContext context = e.Context;

            HttpResponse response = context.Response;

            HttpRequest request = context.Request;

            OutputCacheKeyBuilder keyBuilder = new OutputCacheKeyBuilder(context);

            if (response.StatusCode != 200)
            {
                return;
            }

            DynamicInvoke invoker = new DynamicInvoke(response);

            if (!(bool)invoker.Call("IsBuffered"))
            {
                return;
            }

            if (rule.Debug)
            {
                response.Write("\r\n<!--Served from edge at " + TimeStamp + " by rule " + rule.Name + "-->");

                response.AddHeader("X-Edge-Modified", TimeStamp);
                response.AddHeader("X-Edge-Rule", rule.Name);
            }

            object rawResponse = invoker.Call("GetSnapshot");

            string cacheKey = keyBuilder.GetResponseCacheKey(rule.CacheKeyStamp(context));

            CacheDependency dependency = new CacheDependency(null, new string[] { DependencyKeyFromRule(rule) });

            OutputCacheSnapshot snapshot = new OutputCacheSnapshot(rawResponse);

            DateTime expiry = rule.AbsoluteExpiry;

            HttpRuntime.Cache.Add(cacheKey, snapshot, dependency, expiry, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }
Ejemplo n.º 6
0
        private void OnResolveRequestCache(object sender, EdgeCacheEventArgs e)
        {
            EdgeCacheRule rule = (EdgeCacheRule)sender;

            HttpContext context = e.Context;

            HttpResponse response = context.Response;

            HttpRequest request = context.Request;

            OutputCacheKeyBuilder keyBuilder = new OutputCacheKeyBuilder(context);

            OutputCacheSnapshot snapshot = null;

            foreach (string cacheKey in keyBuilder.EnumerateRequestCacheKeys(rule.CacheKeyStamp(context)))
            {
                snapshot = (OutputCacheSnapshot)HttpRuntime.Cache[cacheKey];

                if (snapshot != null)
                {
                    break;
                }
            }

            if (snapshot != null && snapshot.Response != null)
            {
                bool sendBody = !String.Equals(request.HttpMethod, "HEAD", StringComparison.InvariantCultureIgnoreCase);

                new DynamicInvoke(response)
                .Call("UseSnapshot", snapshot.Response, sendBody);

                response.AddHeader("X-Edge", snapshot.Description);

                OnPreSendSnapshot(new EdgeCacheRuleEventArgs(e.Context, rule));

                context.ApplicationInstance.CompleteRequest();
            }
        }
Ejemplo n.º 7
0
 public EdgeCacheRuleEventArgs(HttpContext context, EdgeCacheRule rule)
     : base(context)
 {
     _rule = rule;
 }
Ejemplo n.º 8
0
 void IEdgeStore.Initialize(EdgeCacheRule rule)
 {
     //Needs to be here otherwise won't get added to snapshot for output cached versions
     rule.PreSendRequestHeaders += OnPreSendRequestHeaders;
 }
Ejemplo n.º 9
0
 void IEdgeStore.Clear(EdgeCacheRule rule)
 {
 }
Ejemplo n.º 10
0
 private static string DependencyKeyFromRule(EdgeCacheRule rule)
 {
     return(String.Format("{0}/{1}", DependencyKeyPrefix, rule.Name));
 }
Ejemplo n.º 11
0
 private void UpdateCacheDependency(EdgeCacheRule rule)
 {
     HttpRuntime.Cache.Insert(DependencyKeyFromRule(rule), new object(), null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
 }
Ejemplo n.º 12
0
 void IEdgeStore.Clear(EdgeCacheRule rule)
 {
     UpdateCacheDependency(rule);
 }