Cache In .Net4.0

.NET4.0中终于将Cache功能从System.Web中迁移了出来:system.runtime.caching.memorycache

 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Caching\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.Caching.dll

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;
}

ref:

http://msdn.microsoft.com/en-us/library/system.runtime.caching.changemonitor.aspx

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx 

原文地址:https://www.cnblogs.com/jianyi0115/p/1735365.html