Esempio n. 1
0
        void OnRawResponseRemoved(string key, object value, CacheItemRemovedReason reason)
        {
            CachedRawResponse c      = value as CachedRawResponse;
            CachedVaryBy      varyby = c != null ? c.VaryBy : null;

            if (varyby == null)
            {
                return;
            }

            List <string>       itemList = varyby.ItemList;
            OutputCacheProvider provider = FindCacheProvider(null);

            itemList.Remove(key);
            provider.Remove(key);

            if (itemList.Count != 0)
            {
                return;
            }

            provider.Remove(varyby.Key);
        }
Esempio n. 2
0
		void DoCacheInsert (HttpContext context, HttpApplication app, HttpResponse response)
		{
			string vary_key = context.Request.FilePath;
			string key;
			OutputCacheProvider provider = FindCacheProvider (app);
			CachedVaryBy varyby = provider.Get (vary_key) as CachedVaryBy;
			CachedRawResponse prev = null;
			bool lookup = true;
			string cacheKey = null, cacheValue = null;
			HttpCachePolicy cachePolicy = response.Cache;
			
			if (varyby == null) {
				varyby = new CachedVaryBy (cachePolicy, vary_key);
				provider.Add (vary_key, varyby, Cache.NoAbsoluteExpiration);
				lookup = false;
				cacheKey = vary_key;
			} 

			key = varyby.CreateKey (vary_key, context);

			if (lookup)
				prev = provider.Get (key) as CachedRawResponse;
			
			if (prev == null) {
				CachedRawResponse c = response.GetCachedResponse ();
				if (c != null) {
					string [] keys = new string [] { vary_key };
					DateTime utcExpiry, absoluteExpiration;
					TimeSpan slidingExpiration;

					c.VaryBy = varyby;
					varyby.ItemList.Add (key);

					if (cachePolicy.Sliding) {
						slidingExpiration = TimeSpan.FromSeconds (cachePolicy.Duration);
						absoluteExpiration = Cache.NoAbsoluteExpiration;
						utcExpiry = DateTime.UtcNow + slidingExpiration;
					} else {
						slidingExpiration = Cache.NoSlidingExpiration;
						absoluteExpiration = cachePolicy.Expires;
						utcExpiry = absoluteExpiration.ToUniversalTime ();
					}

					provider.Set (key, c, utcExpiry);
					HttpRuntime.InternalCache.Insert (key, c, new CacheDependency (null, keys), absoluteExpiration, slidingExpiration,
									  CacheItemPriority.Normal, response_removed);
					cacheValue = key;
				}
			}
			
			if (cacheKey != null) {
				lock (keysCacheLock) {
					if (keysCache == null) {
						BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler (OnBuildManagerRemoveEntry);
						keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
						keysCache.Add (cacheKey, cacheValue);
					} else if (!keysCache.ContainsKey (cacheKey))
						keysCache.Add (cacheKey, cacheValue);
				}
			}
		}
		void DoCacheInsert (HttpContext context)
		{
			string vary_key = context.Request.FilePath;
			string key;
			CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
			CachedRawResponse prev = null;
			bool lookup = true;
#if NET_2_0
			string cacheKey = null, cacheValue = null;
#endif
			
			if (varyby == null) {
				string path = context.Request.MapPath (vary_key);
				string [] files = new string [] { path };
				string [] keys = new string [0];
				varyby = new CachedVaryBy (context.Response.Cache, vary_key);
				context.Cache.Insert (vary_key, varyby,
							      new CacheDependency (files, keys),
							      Cache.NoAbsoluteExpiration,
							      Cache.NoSlidingExpiration,
							      CacheItemPriority.Normal, null);
				lookup = false;
#if NET_2_0
				cacheKey = vary_key;
#endif
			} 

			key = varyby.CreateKey (vary_key, context);

			if (lookup)
				prev = context.InternalCache [key] as CachedRawResponse;
			
			if (prev == null) {
				CachedRawResponse c = context.Response.GetCachedResponse ();
				if (c != null) {
					string [] files = new string [] { };
					string [] keys = new string [] { vary_key };
					bool sliding = context.Response.Cache.Sliding;

					context.InternalCache.Insert (key, c, new CacheDependency (files, keys),
								      (sliding ? Cache.NoAbsoluteExpiration :
								       context.Response.Cache.Expires),
								      (sliding ? TimeSpan.FromSeconds (
									      context.Response.Cache.Duration) :
								       Cache.NoSlidingExpiration),
								      CacheItemPriority.Normal, response_removed);
					c.VaryBy = varyby;
					varyby.ItemList.Add (key);
#if NET_2_0
					cacheValue = key;
#endif
				}
			}
			
#if NET_2_0
			if (cacheKey != null) {
				lock (keysCacheLock) {
					if (keysCache == null) {
						BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler (OnBuildManagerRemoveEntry);
						keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
						keysCache.Add (cacheKey, cacheValue);
					} else if (!keysCache.ContainsKey (cacheKey))
						keysCache.Add (cacheKey, cacheValue);
				}
			}
#endif
		}
		private void DoCacheInsert (HttpContext context)
		{
			string vary_key = context.Request.FilePath;
			string key;
			CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
			CachedRawResponse prev = null;
			bool lookup = true;
			
			if (varyby == null) {
				string path = context.Request.MapPath (vary_key);
				string [] files = new string [] { path };
				string [] keys = new string [0];
				varyby = new CachedVaryBy (context.Response.Cache, vary_key);
				context.Cache.InsertPrivate (vary_key, varyby,
						new CacheDependency (files, keys),
						Cache.NoAbsoluteExpiration,
						Cache.NoSlidingExpiration,
						CacheItemPriority.Normal, null);
				lookup = false;
			} 
			
			key = varyby.CreateKey (vary_key, context);

			if (lookup)
				prev = context.Cache [key] as CachedRawResponse;
			
			if (prev == null) {
				CachedRawResponse c = context.Response.GetCachedResponse ();
				string [] files = new string [] { };
				string [] keys = new string [] { vary_key };
				bool sliding = context.Response.Cache.Sliding;

				context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
						(sliding ? Cache.NoAbsoluteExpiration :
								context.Response.Cache.Expires),
						(sliding ? TimeSpan.FromSeconds (
							context.Response.Cache.Duration) :
								Cache.NoSlidingExpiration),
						CacheItemPriority.Normal, response_removed);
				c.VaryBy = varyby;
				varyby.ItemList.Add (key);
			} 
		}
Esempio n. 5
0
        void DoCacheInsert(HttpContext context, HttpApplication app, HttpResponse response)
        {
            string vary_key = context.Request.FilePath;
            string key;
            OutputCacheProvider provider = FindCacheProvider(app);
            CachedVaryBy        varyby = provider.Get(vary_key) as CachedVaryBy;
            CachedRawResponse   prev = null;
            bool            lookup = true;
            string          cacheKey = null, cacheValue = null;
            HttpCachePolicy cachePolicy = response.Cache;

            if (varyby == null)
            {
                varyby = new CachedVaryBy(cachePolicy, vary_key);
                provider.Add(vary_key, varyby, Cache.NoAbsoluteExpiration);
                lookup   = false;
                cacheKey = vary_key;
            }

            key = varyby.CreateKey(vary_key, context);

            if (lookup)
            {
                prev = provider.Get(key) as CachedRawResponse;
            }

            if (prev == null)
            {
                CachedRawResponse c = response.GetCachedResponse();
                if (c != null)
                {
                    string [] keys = new string [] { vary_key };
                    DateTime  utcExpiry, absoluteExpiration;
                    TimeSpan  slidingExpiration;

                    c.VaryBy = varyby;
                    varyby.ItemList.Add(key);

                    if (cachePolicy.Sliding)
                    {
                        slidingExpiration  = TimeSpan.FromSeconds(cachePolicy.Duration);
                        absoluteExpiration = Cache.NoAbsoluteExpiration;
                        utcExpiry          = DateTime.UtcNow + slidingExpiration;
                    }
                    else
                    {
                        slidingExpiration  = Cache.NoSlidingExpiration;
                        absoluteExpiration = cachePolicy.Expires;
                        utcExpiry          = absoluteExpiration.ToUniversalTime();
                    }

                    provider.Set(key, c, utcExpiry);
                    HttpRuntime.InternalCache.Insert(key, c, new CacheDependency(null, keys), absoluteExpiration, slidingExpiration,
                                                     CacheItemPriority.Normal, response_removed);
                    cacheValue = key;
                }
            }

            if (cacheKey != null)
            {
                lock (keysCacheLock) {
                    if (keysCache == null)
                    {
                        BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler(OnBuildManagerRemoveEntry);
                        keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
                        keysCache.Add(cacheKey, cacheValue);
                    }
                    else if (!keysCache.ContainsKey(cacheKey))
                    {
                        keysCache.Add(cacheKey, cacheValue);
                    }
                }
            }
        }
Esempio n. 6
0
        void OnResolveRequestCache(object o, EventArgs args)
        {
            HttpApplication app     = o as HttpApplication;
            HttpContext     context = app != null ? app.Context : null;

            if (context == null)
            {
                return;
            }

            OutputCacheProvider provider = FindCacheProvider(app);
            string            vary_key   = context.Request.FilePath;
            CachedVaryBy      varyby     = provider.Get(vary_key) as CachedVaryBy;
            string            key;
            CachedRawResponse c;

            if (varyby == null)
            {
                return;
            }

            key = varyby.CreateKey(vary_key, context);
            c   = provider.Get(key) as CachedRawResponse;
            if (c == null)
            {
                return;
            }

            lock (keysCacheLock) {
                string invValue;
                if (entriesToInvalidate != null && entriesToInvalidate.TryGetValue(vary_key, out invValue) && String.Compare(invValue, key, StringComparison.Ordinal) == 0)
                {
                    provider.Remove(vary_key);
                    provider.Remove(key);
                    entriesToInvalidate.Remove(vary_key);
                    return;
                }
            }

            ArrayList callbacks = c.Policy.ValidationCallbacks;

            if (callbacks != null && callbacks.Count > 0)
            {
                bool isValid   = true;
                bool isIgnored = false;

                foreach (Pair p in callbacks)
                {
                    HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
                    object data = p.Second;
                    HttpValidationStatus status = HttpValidationStatus.Valid;

                    try {
                        validate(context, data, ref status);
                    } catch {
                        // MS.NET hides the exception
                        isValid = false;
                        break;
                    }

                    if (status == HttpValidationStatus.Invalid)
                    {
                        isValid = false;
                        break;
                    }
                    else if (status == HttpValidationStatus.IgnoreThisRequest)
                    {
                        isIgnored = true;
                    }
                }

                if (!isValid)
                {
                    OnRawResponseRemoved(key, c, CacheItemRemovedReason.Removed);
                    return;
                }
                else if (isIgnored)
                {
                    return;
                }
            }

            HttpResponse response = context.Response;

            response.ClearContent();
            IList cachedData = c.GetData();

            if (cachedData != null)
            {
                Encoding outEnc = WebEncoding.ResponseEncoding;

                foreach (CachedRawResponse.DataItem d in cachedData)
                {
                    if (d.Length > 0)
                    {
                        response.BinaryWrite(d.Buffer, 0, (int)d.Length);
                        continue;
                    }

                    if (d.Callback == null)
                    {
                        continue;
                    }

                    string s = d.Callback(context);
                    if (s == null || s.Length == 0)
                    {
                        continue;
                    }

                    byte[] bytes = outEnc.GetBytes(s);
                    response.BinaryWrite(bytes, 0, bytes.Length);
                }
            }

            response.ClearHeaders();
            response.SetCachedHeaders(c.Headers);
            response.StatusCode        = c.StatusCode;
            response.StatusDescription = c.StatusDescription;

            app.CompleteRequest();
        }
Esempio n. 7
0
        void DoCacheInsert(HttpContext context)
        {
            string            vary_key = context.Request.FilePath;
            string            key;
            CachedVaryBy      varyby = context.Cache [vary_key] as CachedVaryBy;
            CachedRawResponse prev   = null;
            bool lookup = true;

#if NET_2_0
            string cacheKey = null, cacheValue = null;
#endif

            if (varyby == null)
            {
                string    path  = context.Request.MapPath(vary_key);
                string [] files = new string [] { path };
                string [] keys  = new string [0];
                varyby = new CachedVaryBy(context.Response.Cache, vary_key);
                context.Cache.Insert(vary_key, varyby,
                                     new CacheDependency(files, keys),
                                     Cache.NoAbsoluteExpiration,
                                     Cache.NoSlidingExpiration,
                                     CacheItemPriority.Normal, null);
                lookup = false;
#if NET_2_0
                cacheKey = vary_key;
#endif
            }

            key = varyby.CreateKey(vary_key, context);

            if (lookup)
            {
                prev = context.InternalCache [key] as CachedRawResponse;
            }

            if (prev == null)
            {
                CachedRawResponse c = context.Response.GetCachedResponse();
                if (c != null)
                {
                    string [] files   = new string [] { };
                    string [] keys    = new string [] { vary_key };
                    bool      sliding = context.Response.Cache.Sliding;

                    context.InternalCache.Insert(key, c, new CacheDependency(files, keys),
                                                 (sliding ? Cache.NoAbsoluteExpiration :
                                                  context.Response.Cache.Expires),
                                                 (sliding ? TimeSpan.FromSeconds(
                                                      context.Response.Cache.Duration) :
                                                  Cache.NoSlidingExpiration),
                                                 CacheItemPriority.Normal, response_removed);
                    c.VaryBy = varyby;
                    varyby.ItemList.Add(key);
#if NET_2_0
                    cacheValue = key;
#endif
                }
            }

#if NET_2_0
            if (cacheKey != null)
            {
                lock (keysCacheLock) {
                    if (keysCache == null)
                    {
                        BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler(OnBuildManagerRemoveEntry);
                        keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
                        keysCache.Add(cacheKey, cacheValue);
                    }
                    else if (!keysCache.ContainsKey(cacheKey))
                    {
                        keysCache.Add(cacheKey, cacheValue);
                    }
                }
            }
#endif
        }