日期:2014-05-18  浏览次数:20981 次

Winform中是否有类似ASP.NET中的Cache专门的缓存设施
如题,知道的请明示,谢谢。

------解决方案--------------------
C# code

private void Form1_Load(object sender, EventArgs e)
        {
            //runTime = new HttpRuntime();
            cache = HttpRuntime.Cache;
            cache.Add("something", "something", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
            string something = cache["something"] as string;
        }

------解决方案--------------------
确保你的winform工程使用.net(而不是“.net客户端”),然后引用system.web.dll即可。
C# code
System.Web.Runtime.HttRuntime

------解决方案--------------------
System.Web.Runtime.HttpRuntime


通过 HttpRuntime.Cache 就可以访问Cache完整功能,将对象缓存在这里。
------解决方案--------------------
为啥?

最简单的一个功能,

C# code
HttpRuntime.Cache.Insert(key, MyConfigObject, new CacheDependency(filePath));

------解决方案--------------------
正在看的一本书里,说.net 4.0 里推荐用System.Runtime.Caching.dll(命名空间是System.Runtime.Caching),这里抄一点代码:

ObjectCache cache = MemoryCache.Default;

string mystring = cache["MyString"] as string;

if(String.IsNullOrEmpty(mystring))
{
List<string> userFilePath = new List<string>();
userFilePath.Add(@"c:\my.xml");
CacheItemPolicy policy = new CacheItemPolicy();
policy.ChangeMonitors.Add(new HostFileChangeMonitor(userFilePath);

//取数据
...
cache.Set("MyString", "abcd", policy);
}
这里的HostFileChangeMonitor可以监视目录或者文件的变化,更新cache,另外还有SqlChangeMonitor,顾名思义应该是监视数据库变化的,具体没用过。
System.Web.Caching.Cache仍然可用,但是推荐使用System.Runtime.Caching,因为最新的增强功能都将放在这个dll里。
------解决方案--------------------
HttpRuntime这个东西唯一不放心的就是他是如何被创建和管理的 因为我在代码里面直接就写了HttpRuntime.Cache 没有报空异常 不知道有没有大牛可以解释下
------解决方案--------------------
探讨

为啥?

最简单的一个功能,

C# code
HttpRuntime.Cache.Insert(key, MyConfigObject, new CacheDependency(filePath));


你自己写一遍,又怎么比得上人家只写一句话?!而且这还是最简单的一句。