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); }
private static OutputCacheEntry Convert(CachedRawResponse cachedRawResponse, string depKey, string[] fileDependencies) { List <HeaderElement> headerElements = null; ArrayList headers = cachedRawResponse._rawResponse.Headers; int count = (headers != null) ? headers.Count : 0; for (int i = 0; i < count; i++) { if (headerElements == null) { headerElements = new List <HeaderElement>(count); } HttpResponseHeader h = (HttpResponseHeader)(headers[i]); headerElements.Add(new HeaderElement(h.Name, h.Value)); } List <ResponseElement> responseElements = null; ArrayList buffers = cachedRawResponse._rawResponse.Buffers; count = (buffers != null) ? buffers.Count : 0; for (int i = 0; i < count; i++) { if (responseElements == null) { responseElements = new List <ResponseElement>(count); } IHttpResponseElement elem = buffers[i] as IHttpResponseElement; if (elem is HttpFileResponseElement) { HttpFileResponseElement fileElement = elem as HttpFileResponseElement; responseElements.Add(new FileResponseElement(fileElement.FileName, fileElement.Offset, elem.GetSize())); } else if (elem is HttpSubstBlockResponseElement) { HttpSubstBlockResponseElement substElement = elem as HttpSubstBlockResponseElement; responseElements.Add(new SubstitutionResponseElement(substElement.Callback)); } else { byte[] b = elem.GetBytes(); long length = (b != null) ? b.Length : 0; responseElements.Add(new MemoryResponseElement(b, length)); } } OutputCacheEntry oce = new OutputCacheEntry( cachedRawResponse._cachedVaryId, cachedRawResponse._settings, cachedRawResponse._kernelCacheUrl, depKey, fileDependencies, cachedRawResponse._rawResponse.StatusCode, cachedRawResponse._rawResponse.StatusDescription, headerElements, responseElements ); return(oce); }
private static CachedRawResponse Convert(OutputCacheEntry oce) { ArrayList headers = null; if ((oce.HeaderElements != null) && (oce.HeaderElements.Count > 0)) { headers = new ArrayList(oce.HeaderElements.Count); for (int i = 0; i < oce.HeaderElements.Count; i++) { HttpResponseHeader header = new HttpResponseHeader(oce.HeaderElements[i].Name, oce.HeaderElements[i].Value); headers.Add(header); } } ArrayList buffers = null; if ((oce.ResponseElements != null) && (oce.ResponseElements.Count > 0)) { buffers = new ArrayList(oce.ResponseElements.Count); for (int j = 0; j < oce.ResponseElements.Count; j++) { ResponseElement element = oce.ResponseElements[j]; IHttpResponseElement element2 = null; if (element is FileResponseElement) { HttpContext current = HttpContext.Current; HttpWorkerRequest request = (current != null) ? current.WorkerRequest : null; bool supportsLongTransmitFile = (request != null) && request.SupportsLongTransmitFile; bool isImpersonating = ((current != null) && current.IsClientImpersonationConfigured) || HttpRuntime.IsOnUNCShareInternal; FileResponseElement element3 = (FileResponseElement)element; element2 = new HttpFileResponseElement(element3.Path, element3.Offset, element3.Length, isImpersonating, supportsLongTransmitFile); } else if (element is MemoryResponseElement) { MemoryResponseElement element4 = (MemoryResponseElement)element; int size = System.Convert.ToInt32(element4.Length); element2 = new HttpResponseBufferElement(element4.Buffer, size); } else { if (!(element is SubstitutionResponseElement)) { throw new NotSupportedException(); } SubstitutionResponseElement element5 = (SubstitutionResponseElement)element; element2 = new HttpSubstBlockResponseElement(element5.Callback); } buffers.Add(element2); } } else { buffers = new ArrayList(); } return(new CachedRawResponse(new HttpRawResponse(oce.StatusCode, oce.StatusDescription, headers, buffers, false), oce.Settings, oce.KernelCacheUrl, oce.CachedVaryId)); }
private static CachedRawResponse Convert(OutputCacheEntry oce) { ArrayList headers = null; if ((oce.HeaderElements != null) && (oce.HeaderElements.Count > 0)) { headers = new ArrayList(oce.HeaderElements.Count); for (int i = 0; i < oce.HeaderElements.Count; i++) { HttpResponseHeader header = new HttpResponseHeader(oce.HeaderElements[i].Name, oce.HeaderElements[i].Value); headers.Add(header); } } ArrayList buffers = null; if ((oce.ResponseElements != null) && (oce.ResponseElements.Count > 0)) { buffers = new ArrayList(oce.ResponseElements.Count); for (int j = 0; j < oce.ResponseElements.Count; j++) { ResponseElement element = oce.ResponseElements[j]; IHttpResponseElement element2 = null; if (element is FileResponseElement) { HttpContext current = HttpContext.Current; HttpWorkerRequest request = (current != null) ? current.WorkerRequest : null; bool supportsLongTransmitFile = (request != null) && request.SupportsLongTransmitFile; bool isImpersonating = ((current != null) && current.IsClientImpersonationConfigured) || HttpRuntime.IsOnUNCShareInternal; FileResponseElement element3 = (FileResponseElement) element; element2 = new HttpFileResponseElement(element3.Path, element3.Offset, element3.Length, isImpersonating, supportsLongTransmitFile); } else if (element is MemoryResponseElement) { MemoryResponseElement element4 = (MemoryResponseElement) element; int size = System.Convert.ToInt32(element4.Length); element2 = new HttpResponseBufferElement(element4.Buffer, size); } else { if (!(element is SubstitutionResponseElement)) { throw new NotSupportedException(); } SubstitutionResponseElement element5 = (SubstitutionResponseElement) element; element2 = new HttpSubstBlockResponseElement(element5.Callback); } buffers.Add(element2); } } else { buffers = new ArrayList(); } return new CachedRawResponse(new HttpRawResponse(oce.StatusCode, oce.StatusDescription, headers, buffers, false), oce.Settings, oce.KernelCacheUrl, oce.CachedVaryId); }
// 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); }
private static CachedRawResponse Convert(OutputCacheEntry oce) { ArrayList headers = null; if (oce.HeaderElements != null && oce.HeaderElements.Count > 0) { headers = new ArrayList(oce.HeaderElements.Count); for (int i = 0; i < oce.HeaderElements.Count; i++) { HttpResponseHeader h = new HttpResponseHeader(oce.HeaderElements[i].Name, oce.HeaderElements[i].Value); headers.Add(h); } } ArrayList buffers = null; if (oce.ResponseElements != null && oce.ResponseElements.Count > 0) { buffers = new ArrayList(oce.ResponseElements.Count); for (int i = 0; i < oce.ResponseElements.Count; i++) { ResponseElement re = oce.ResponseElements[i]; IHttpResponseElement elem = null; if (re is FileResponseElement) { HttpContext context = HttpContext.Current; HttpWorkerRequest wr = (context != null) ? context.WorkerRequest : null; bool supportsLongTransmitFile = (wr != null && wr.SupportsLongTransmitFile); bool isImpersonating = ((context != null && context.IsClientImpersonationConfigured) || HttpRuntime.IsOnUNCShareInternal); FileResponseElement fre = (FileResponseElement)re; // DevDiv #21203: Need to verify permission to access the requested file since handled by native code. HttpRuntime.CheckFilePermission(fre.Path); elem = new HttpFileResponseElement(fre.Path, fre.Offset, fre.Length, isImpersonating, supportsLongTransmitFile); } else if (re is MemoryResponseElement) { MemoryResponseElement mre = (MemoryResponseElement)re; int size = System.Convert.ToInt32(mre.Length); elem = new HttpResponseBufferElement(mre.Buffer, size); } else if (re is SubstitutionResponseElement) { SubstitutionResponseElement sre = (SubstitutionResponseElement)re; elem = new HttpSubstBlockResponseElement(sre.Callback); } else { throw new NotSupportedException(); } buffers.Add(elem); } } else { buffers = new ArrayList(); } HttpRawResponse rawResponse = new HttpRawResponse(oce.StatusCode, oce.StatusDescription, headers, buffers, false /*hasSubstBlocks*/); CachedRawResponse cachedRawResponse = new CachedRawResponse(rawResponse, oce.Settings, oce.KernelCacheUrl, oce.CachedVaryId); return cachedRawResponse; }
private static OutputCacheEntry Convert(CachedRawResponse cachedRawResponse, string depKey, string[] fileDependencies) { List<HeaderElement> headerElements = null; ArrayList headers = cachedRawResponse._rawResponse.Headers; int count = (headers != null) ? headers.Count : 0; for (int i = 0; i < count; i++) { if (headerElements == null) { headerElements = new List<HeaderElement>(count); } HttpResponseHeader h = (HttpResponseHeader)(headers[i]); headerElements.Add(new HeaderElement(h.Name, h.Value)); } List<ResponseElement> responseElements = null; ArrayList buffers = cachedRawResponse._rawResponse.Buffers; count = (buffers != null) ? buffers.Count : 0; for (int i = 0; i < count; i++) { if (responseElements == null) { responseElements = new List<ResponseElement>(count); } IHttpResponseElement elem = buffers[i] as IHttpResponseElement; if (elem is HttpFileResponseElement) { HttpFileResponseElement fileElement = elem as HttpFileResponseElement; responseElements.Add(new FileResponseElement(fileElement.FileName, fileElement.Offset, elem.GetSize())); } else if (elem is HttpSubstBlockResponseElement) { HttpSubstBlockResponseElement substElement = elem as HttpSubstBlockResponseElement; responseElements.Add(new SubstitutionResponseElement(substElement.Callback)); } else { byte[] b = elem.GetBytes(); long length = (b != null) ? b.Length : 0; responseElements.Add(new MemoryResponseElement(b, length)); } } OutputCacheEntry oce = new OutputCacheEntry( cachedRawResponse._cachedVaryId, cachedRawResponse._settings, cachedRawResponse._kernelCacheUrl, depKey, fileDependencies, cachedRawResponse._rawResponse.StatusCode, cachedRawResponse._rawResponse.StatusDescription, headerElements, responseElements ); return oce; }
// insert cached vary or output cache entry internal static void InsertResponse(String cachedVaryKey, CachedVary cachedVary, String rawResponseKey, CachedRawResponse rawResponse, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp) { // if the provider is undefined or the fragment can't be inserted in the // provider, insert it in the internal cache. OutputCacheProvider provider = GetProvider(HttpContext.Current); // // CachedVary can be serialized. // CachedRawResponse is not always serializable. // bool useProvider = (provider != null); if (useProvider) { bool canUseProvider = (IsSubstBlockSerializable(rawResponse._rawResponse) && rawResponse._settings.IsValidationCallbackSerializable() && slidingExp == Cache.NoSlidingExpiration && (dependencies == null || dependencies.IsFileDependency())); if (useProvider && !canUseProvider) { throw new ProviderException(SR.GetString(SR.Provider_does_not_support_policy_for_responses, provider.Name)); } } #if DBG bool cachedVaryPutInCache = (cachedVary != null); #endif if (cachedVary != null) { /* * Add the CachedVary item so that a request will know * which headers are needed to issue another request. * * Use the Add method so that we guarantee we only use * a single CachedVary and don't overwrite existing ones. */ CachedVary cachedVaryInCache; if (!useProvider) { cachedVaryInCache = OutputCache.UtcAdd(cachedVaryKey, cachedVary); } else { cachedVaryInCache = (CachedVary)provider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration); } if (cachedVaryInCache != null) { if (!cachedVary.Equals(cachedVaryInCache)) { if (!useProvider) { HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary); } else { provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration); } } else { cachedVary = cachedVaryInCache; #if DBG cachedVaryPutInCache = false; #endif } } if (!useProvider) { AddCacheKeyToDependencies(ref dependencies, cachedVaryKey); } // not all caches support cache key dependencies, but we can use a "change number" to associate // the ControlCachedVary and the PartialCachingCacheEntry rawResponse._cachedVaryId = cachedVary.CachedVaryId; } // Now insert into the cache (use cache provider if possible, otherwise use internal cache) if (!useProvider) { HttpRuntime.CacheInternal.UtcInsert(rawResponseKey, rawResponse, dependencies, absExp, slidingExp, CacheItemPriority.Normal, s_entryRemovedCallback); IncrementCount(); PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_ENTRIES); PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_TURNOVER_RATE); } else { string depKey = null; string[] fileDeps = null; if (dependencies != null) { depKey = OUTPUTCACHE_KEYPREFIX_DEPENDENCIES + dependencies.GetUniqueID(); fileDeps = dependencies.GetFileDependencies(); } OutputCacheEntry oce = Convert(rawResponse, depKey, fileDeps); provider.Set(rawResponseKey, oce, absExp); if (dependencies != null) { // use Add and dispose dependencies if there's already one in the cache Object d = HttpRuntime.CacheInternal.UtcAdd(depKey, new DependencyCacheEntry(rawResponseKey, oce.KernelCacheUrl, provider.Name), dependencies, absExp, Cache.NoSlidingExpiration, CacheItemPriority.Normal, s_dependencyRemovedCallback); if (d != null) { dependencies.Dispose(); } } } #if DBG string cachedVaryType = (cachedVaryPutInCache) ? "CachedVary" : ""; string providerUsed = (useProvider) ? provider.Name : "CacheInternal"; Debug.Trace("OutputCache", "InsertResposne(" + cachedVaryKey + ", " + cachedVaryType + ", " + rawResponseKey + ", CachedRawResponse, ...) -->" + providerUsed); #endif }
private static CachedRawResponse Convert(OutputCacheEntry oce) { ArrayList headers = null; if (oce.HeaderElements != null && oce.HeaderElements.Count > 0) { headers = new ArrayList(oce.HeaderElements.Count); for (int i = 0; i < oce.HeaderElements.Count; i++) { HttpResponseHeader h = new HttpResponseHeader(oce.HeaderElements[i].Name, oce.HeaderElements[i].Value); headers.Add(h); } } ArrayList buffers = null; if (oce.ResponseElements != null && oce.ResponseElements.Count > 0) { buffers = new ArrayList(oce.ResponseElements.Count); for (int i = 0; i < oce.ResponseElements.Count; i++) { ResponseElement re = oce.ResponseElements[i]; IHttpResponseElement elem = null; if (re is FileResponseElement) { HttpContext context = HttpContext.Current; HttpWorkerRequest wr = (context != null) ? context.WorkerRequest : null; bool supportsLongTransmitFile = (wr != null && wr.SupportsLongTransmitFile); bool isImpersonating = ((context != null && context.IsClientImpersonationConfigured) || HttpRuntime.IsOnUNCShareInternal); FileResponseElement fre = (FileResponseElement)re; // DevDiv #21203: Need to verify permission to access the requested file since handled by native code. HttpRuntime.CheckFilePermission(fre.Path); elem = new HttpFileResponseElement(fre.Path, fre.Offset, fre.Length, isImpersonating, supportsLongTransmitFile); } else if (re is MemoryResponseElement) { MemoryResponseElement mre = (MemoryResponseElement)re; int size = System.Convert.ToInt32(mre.Length); elem = new HttpResponseBufferElement(mre.Buffer, size); } else if (re is SubstitutionResponseElement) { SubstitutionResponseElement sre = (SubstitutionResponseElement)re; elem = new HttpSubstBlockResponseElement(sre.Callback); } else { throw new NotSupportedException(); } buffers.Add(elem); } } else { buffers = new ArrayList(); } HttpRawResponse rawResponse = new HttpRawResponse(oce.StatusCode, oce.StatusDescription, headers, buffers, false /*hasSubstBlocks*/); CachedRawResponse cachedRawResponse = new CachedRawResponse(rawResponse, oce.Settings, oce.KernelCacheUrl, oce.CachedVaryId); return(cachedRawResponse); }
internal static void InsertResponse(string cachedVaryKey, CachedVary cachedVary, string rawResponseKey, CachedRawResponse rawResponse, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp) { OutputCacheProvider provider = GetProvider(HttpContext.Current); bool flag = provider != null; if (flag) { bool flag2 = ((IsSubstBlockSerializable(rawResponse._rawResponse) && rawResponse._settings.IsValidationCallbackSerializable()) && (slidingExp == Cache.NoSlidingExpiration)) && ((dependencies == null) || dependencies.IsFileDependency()); if (flag && !flag2) { throw new ProviderException(System.Web.SR.GetString("Provider_does_not_support_policy_for_responses", new object[] { provider.Name })); } } if (cachedVary != null) { CachedVary vary; if (!flag) { vary = UtcAdd(cachedVaryKey, cachedVary); } else { vary = (CachedVary)provider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration); } if (vary != null) { if (!cachedVary.Equals(vary)) { if (!flag) { HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary); } else { provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration); } } else { cachedVary = vary; } } if (!flag) { AddCacheKeyToDependencies(ref dependencies, cachedVaryKey); } rawResponse._cachedVaryId = cachedVary.CachedVaryId; } if (!flag) { HttpRuntime.CacheInternal.UtcInsert(rawResponseKey, rawResponse, dependencies, absExp, slidingExp, CacheItemPriority.Normal, s_entryRemovedCallback); IncrementCount(); PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_ENTRIES); PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_TURNOVER_RATE); } else { string depKey = null; string[] fileDependencies = null; if (dependencies != null) { depKey = "aD" + dependencies.GetUniqueID(); fileDependencies = dependencies.GetFileDependencies(); } OutputCacheEntry entry = Convert(rawResponse, depKey, fileDependencies); provider.Set(rawResponseKey, entry, absExp); if ((dependencies != null) && (HttpRuntime.CacheInternal.UtcAdd(depKey, new DependencyCacheEntry(rawResponseKey, entry.KernelCacheUrl, provider.Name), dependencies, absExp, Cache.NoSlidingExpiration, CacheItemPriority.Normal, s_dependencyRemovedCallback) != null)) { dependencies.Dispose(); } } }