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); }
void IEdgeStore.Initialize(EdgeCacheRule rule) { UpdateCacheDependency(rule); rule.ResolveRequestCache += OnResolveRequestCache; rule.UpdateRequestCache += OnUpdateRequestCache; }
public bool Include(EdgeCacheRule rule, OriginUrl url) { RegexEdgeFilterGroup group; if (_groups.TryGetValue(rule.Name, out group)) { return(group.Include(url)); } return(false); }
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); }
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); }
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(); } }
public EdgeCacheRuleEventArgs(HttpContext context, EdgeCacheRule rule) : base(context) { _rule = rule; }
void IEdgeStore.Initialize(EdgeCacheRule rule) { //Needs to be here otherwise won't get added to snapshot for output cached versions rule.PreSendRequestHeaders += OnPreSendRequestHeaders; }
void IEdgeStore.Clear(EdgeCacheRule rule) { }
private static string DependencyKeyFromRule(EdgeCacheRule rule) { return(String.Format("{0}/{1}", DependencyKeyPrefix, rule.Name)); }
private void UpdateCacheDependency(EdgeCacheRule rule) { HttpRuntime.Cache.Insert(DependencyKeyFromRule(rule), new object(), null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); }
void IEdgeStore.Clear(EdgeCacheRule rule) { UpdateCacheDependency(rule); }