普通缓存类和MemoryCache类有什么区别?
缓存是指存储在内存中的数据,然后为MemoryCache提供额外的类?
MemoryCache类的用途是什么以及何时使用而不是普通的缓存类?
只看下面的示例代码
private void btnGet_Click(object sender, EventArgs e)
{
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;
}
上面的代码是做什么的?它正在监控文件内容的变化吗?
喜欢了解流程。感谢
答案 0 :(得分:4)
HttpRuntime.Cache获取当前应用程序的Cache
见这里
msdn
MemoryCache是存储在内存中的缓存。 表示实现内存高速缓存的类型 msdn
这是一个出色的博客,可以清除您的所有疑虑blog 这个博客只有几行。
msdn说this
The Cache class is not intended for use outside of ASP.NET applications. It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.
尽管Microsoft始终坚持认为ASP.NET缓存不适合在Web之外使用。但是很多人仍然坚持使用.NET 2.0和.NET 3.5,并且需要处理一些事情。
Microsoft最终在最新版本的.NET Framework中实现了一个抽象的ObjectCache类,以及一个在非Web设置中为内存目的继承和实现ObjectCache的MemoryCache实现。 System.Runtime.Caching.ObjectCache位于System.Runtime.Caching.dll程序集中。它是一个抽象类,它声明了与ASP.NET缓存中基本相同的.NET 1.0样式接口。
System.Runtime.Caching.MemoryCache是ObjectCache的内存实现,与ASP.NET缓存非常相似,但有一些变化。