Пример #1
0
        internal static object Get(string key)
        {
            object obj2 = null;
            OutputCacheProvider provider = GetProvider(HttpContext.Current);

            if (provider != null)
            {
                obj2 = provider.Get(key);
                OutputCacheEntry oce = obj2 as OutputCacheEntry;
                if (oce != null)
                {
                    if (HasDependencyChanged(false, oce.DependenciesKey, oce.Dependencies, oce.KernelCacheUrl, key, provider.Name))
                    {
                        RemoveFromProvider(key, provider.Name);
                        return(null);
                    }
                    obj2 = Convert(oce);
                }
            }
            if (obj2 == null)
            {
                obj2 = HttpRuntime.CacheInternal.Get(key);
            }
            return(obj2);
        }
Пример #2
0
        internal static object GetFragment(string key, string providerName)
        {
            object obj2 = null;
            OutputCacheProvider fragmentProvider = GetFragmentProvider(providerName);

            if (fragmentProvider != null)
            {
                obj2 = fragmentProvider.Get(key);
                PartialCachingCacheEntry entry = obj2 as PartialCachingCacheEntry;
                if ((entry != null) && HasDependencyChanged(true, entry._dependenciesKey, entry._dependencies, null, key, fragmentProvider.Name))
                {
                    RemoveFragment(key, fragmentProvider.Name);
                    return(null);
                }
            }
            if (obj2 == null)
            {
                obj2 = HttpRuntime.CacheInternal.Get(key);
            }
            return(obj2);
        }
Пример #3
0
        // lookup fragment
        internal static Object GetFragment(String key, String providerName)
        {
            // if providerName is null, use default provider.
            // if providerName is not null, get it from the provider collection.
            // if it's not in the provider or the default provider is undefined,
            // check the internal cache (we don't know where it is).
            Object result = null;
            OutputCacheProvider provider = GetFragmentProvider(providerName);

            if (provider != null)
            {
                result = provider.Get(key);
                PartialCachingCacheEntry fragment = result as PartialCachingCacheEntry;
                if (fragment != null)
                {
                    if (HasDependencyChanged(true /*isFragment*/, fragment._dependenciesKey, fragment._dependencies, null /*kernelKey*/, key, provider.Name))
                    {
                        OutputCache.RemoveFragment(key, provider.Name);
#if DBG
                        Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> null, " + providerName);
#endif
                        return(null);
                    }
                }
            }

            if (result == null)
            {
                result = HttpRuntime.CacheInternal.Get(key);
#if DBG
                string typeName = (result != null) ? result.GetType().Name : "null";
                Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> " + typeName + ", CacheInternal");
            }
            else
            {
                Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> " + result.GetType().Name + ", " + providerName);
#endif
            }
            return(result);
        }
Пример #4
0
        // lookup cached vary
        // lookup entry
        // lookup entry for content-encoding
        internal static Object Get(String key)
        {
            // if it's not in the provider or the default provider is undefined,
            // check the internal cache (we don't know where it is).
            Object result = null;
            OutputCacheProvider provider = GetProvider(HttpContext.Current);

            if (provider != null)
            {
                result = provider.Get(key);
                OutputCacheEntry oce = result as OutputCacheEntry;
                if (oce != null)
                {
                    if (HasDependencyChanged(false /*isFragment*/, oce.DependenciesKey, oce.Dependencies, oce.KernelCacheUrl, key, provider.Name))
                    {
                        OutputCache.RemoveFromProvider(key, provider.Name);
#if DBG
                        Debug.Trace("OutputCache", "Get(" + key + ") --> null, " + provider.Name);
#endif
                        return(null);
                    }
                    result = Convert(oce);
                }
            }
            if (result == null)
            {
                result = HttpRuntime.CacheInternal.Get(key);
#if DBG
                string typeName = (result != null) ? result.GetType().Name : "null";
                Debug.Trace("OutputCache", "Get(" + key + ") --> " + typeName + ", CacheInternal");
            }
            else
            {
                Debug.Trace("OutputCache", "Get(" + key + ") --> " + result.GetType().Name + ", " + provider.Name);
#endif
            }
            return(result);
        }
Пример #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);
                    }
                }
            }
        }
Пример #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();
        }