现在的位置: 首页 > 综合 > 正文

Cache In .Net4.0

2012年09月16日 ⁄ 综合 ⁄ 共 857字 ⁄ 字号 评论关闭

.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 

抱歉!评论已关闭.