在asp.net4.0后我們可以實現自己的OutputCacheProvider來控制緩存的位置了,但是我發現很多人多OutputCacheProvider的調用並不是很清楚。首先我們要知道緩存是在哪里注冊的。答案是OutputCacheModule
void IHttpModule.Init(HttpApplication app)
{
if (RuntimeConfig.GetAppConfig().OutputCache.EnableOutputCache)
{
app.ResolveRequestCache += new EventHandler(this.OnEnter);
app.UpdateRequestCache += new EventHandler(this.OnLeave);
}
}
(RuntimeConfig.GetAppConfig().OutputCache.EnableOutputCache這句很明白檢查我們的配置,看看你是否啟用緩存。OnEnter是在處理前讀取緩存數據,處理后設置緩存。
那么先讓我們看看OnEnter的方法把,我把里面多數代碼刪除 只留主要的結果。
internal void OnEnter(object source, EventArgs eventArgs)
{
this._key = null;
string str;
HttpResponse response = context.Response;
this._key = str = this.CreateOutputCachedItemKey(context, null);
object obj2 = OutputCache.Get(str);
if (obj2 != null)
{
CachedVary cachedVary = obj2 as CachedVary;
if (cachedVary != null)
{
str = this.CreateOutputCachedItemKey(context, cachedVary);
obj2 = OutputCache.Get(str);}
CachedRawResponse response2 = (CachedRawResponse) obj2;
HttpCachePolicySettings settings = response2._settings;
HttpRawResponse rawResponse = response2._rawResponse;
response.UseSnapshot(rawResponse, sendBody);
application.CompleteRequest();
return;
}
return;
}
}
這里有兩次調用obj2 = OutputCache.Get(str),我們來分析一下吧,第一次取出來的數據被轉化為cachedVary,這個東西就是保存我們OutputCache中的那些屬性配置的一個實例,所以OutputCache不同的屬性就會產生不同的cachedVary值,而第二次讀取的obj2才是我們真正需要保存的東西,把它取到后做一系列的處理最后結束此次http請求。
如取值是取2次那么設置也一定是設置2次了。
internal void OnLeave(object source, EventArgs eventArgs)
{
CachedVary vary;
string str;
vary = new CachedVary(varyByContentEncodings, varyByHeaders, varyByParams, varyByAllParams, currentSettings.VaryByCustom);
str = this.CreateOutputCachedItemKey(context, vary);
HttpRawResponse snapshot = response.GetSnapshot();
string kernelCacheUrl = response.SetupKernelCaching(null);
Guid cachedVaryId = (vary != null) ? vary.CachedVaryId : Guid.Empty;
CachedRawResponse rawResponse = new CachedRawResponse(snapshot, currentSettings, kernelCacheUrl, cachedVaryId);
CacheDependency dependencies = response.CreateCacheDependencyForResponse();
OutputCache.InsertResponse(this._key, vary, str, rawResponse, dependencies, noAbsoluteExpiration, noSlidingExpiration);
}
}
在這里我們看到了CachedVary、CachedRawResponse的兩個實例,在這里我們可以確定先前第二次調用obj2 = OutputCache.Get(str)的obj2應該是一個CachedRawResponse實例。而OutputCache的InsertResponse方法:
internal static void InsertResponse(string cachedVaryKey, CachedVary cachedVary, string rawResponseKey, CachedRawResponse rawResponse, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp)
{
OutputCacheProvider provider = GetProvider(HttpContext.Current);
provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
provider.Set(rawResponseKey, entry, absExp);
}
我相信大家看到這里就明白了,OutputCache的保存和讀取都是操作2個實例,一個是OutputCache配置實例CachedVary ,另一個是真正的數據流CachedRawResponse 。
看到OutputCache的InsertResponse方法這里提到了OutputCacheProvider provider = GetProvider(HttpContext.Current)而在OutputCacheProvider 的Get方法中也調用了這句
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; }
而OutputCache的GetProvider的方法干什么的我想我就不提了,相信大家都會明白。
所以要實現自定義的緩存,可以有兩種方案分別是實現自己的OutputCacheProvider和注冊自己OutputCacheModule
public class MemoryCacheProvider : OutputCacheProvider { public override object Add(string key, object entry, DateTime utcExpiry) { return null; } public override object Get(string key) { return null; } public override void Set(string key, object entry, DateTime utcExpiry) { } public override void Remove(string key) { } } public class CustOutputCacheModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache); context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache); } void context_UpdateRequestCache(object sender, EventArgs e) { } void context_ResolveRequestCache(object sender, EventArgs e) { } }
相應的配置是
<caching>
<outputCache defaultProvider="ChannelInMemory">
<providers>
<add name="ChannelInMemory" type="MvcApp.MemoryCacheProvider,MvcApp" />
</providers>
</outputCache>
</caching>
<httpModules>
<remove name="OutputCache"/>
<add name="OutputCache" type="MvcApp.CustOutputCacheModule" />
</httpModules>