緩存在ASP.NET看起來像它使用某種關聯數組:通過ASP.NET緩存對象鍵循環
// Insert some data into the cache:
Cache.Insert("TestCache", someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");
// But, can be done associatively ...
someValue = Cache["TestCache"];
// Also, null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];
正如你所看到的,在緩存對象上執行空檢查是非常有用的。
但我想實現一個緩存清除功能,可以清除緩存值 ,其中我不知道整個鍵名。由於在這裏似乎有一個關聯 陣列,它應該有可能(?)
任何人都可以幫助我找出一種方法循環存儲的緩存鍵和 執行他們的簡單邏輯?下面是我所追求的:
static void DeleteMatchingCacheKey(string keyName) {
// This foreach implementation doesn't work by the way ...
foreach(Cache as c) {
if(c.Key.Contains(keyName)) {
Cache.Remove(c);
}
}
}
緩存是你的控制之下 - 你爲什麼不知道的東西,都在那裏的名字? – 2010-11-29 10:49:06