【发布时间】:2015-09-03 18:14:58
【问题描述】:
普通缓存类和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;
}
上面的代码是做什么的?是否监控文件内容变化?
【问题讨论】:
-
可能重复。 “HttpRuntime.Cache 和 MemoryCache 之间的主要区别在于后者已被更改,以使其可供非 ASP.NET 应用程序的 .NET Framework 应用程序使用。” stackoverflow.com/questions/13704000/…
标签: asp.net memorycache