雖然其他人所說內置的ASP.NET緩存(System.Web.Caching),請注意,.NET 4.0引入了一個全新的緩存框架設計到System.Web.Caching命名空間外工作:
System.Runtime.Caching
http://msdn.microsoft.com/en-us/library/system.runtime.caching(VS.100).aspx
現在,這更是一個禽獸不如簡單System.Web.Caching的緩存表項。但是,您獲得了擁有多個緩存存儲,鎖定緩存對象/鍵以及創建自己的外部緩存提供程序的可擴展性的優勢 - 例如Microsoft的新Velocity distributed caching system。它配有內置的MemoryCache提供程序。
這真的不是很難使用。直接從MSDN對第內置的MemoryCache提供商(同樣,你可以實現你自己的)的WinForms應用程序使用它(您想更改代碼爲您的Web應用程序):
http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
List<string> filePaths = new List<string>();
filePaths.Add("c:\\cache\\example.txt");
policy.ChangeMonitors.Add(new
HostFileChangeMonitor(filePaths));
// Fetch the file contents.
fileContents =
File.ReadAllText("c:\\cache\\example.txt");
cache.Set("filecontents", fileContents, policy);
}
Label1.Text = fileContents;
我已經實現了NCache(如上所述)以及Memcached。我可以告訴你,微軟的Velocity真的是分區數據並在緩存分區內設置冗餘(非常酷)的方法。更何況,它是免費的!