广东电白建设集团有限公司官方网站,网页设计总结收获和体会,wordpress中常用插件安装包,室内装修网站模板以前写asp.net时用HttpContext.Current.Cache存缓存很好用#xff0c;今天写了一个windows服务程序#xff0c;HttpContext.Current.Cache存缓存的时候还好#xff0c;取的时候一直报错“未将对象引用到实例”很郁闷#xff0c;查询了一下资料才明白引用程序缓存要用HttpRu… 以前写asp.net时用HttpContext.Current.Cache存缓存很好用今天写了一个windows服务程序HttpContext.Current.Cache存缓存的时候还好取的时候一直报错“未将对象引用到实例”很郁闷查询了一下资料才明白引用程序缓存要用HttpRuntime.Cache... 我们先看MSDN上的解释 HttpContext.Current.Cache为当前 HTTP 请求获取Cache对象。 HttpRuntime.Cache获取当前应用程序的Cache。
附带的写了一个操作缓存的通用类在应用程序中使用如果要在asp.net中有只需把HttpRuntime.Cache改为HttpContext.Current.Cache即可代码如下 代码 using System;/// summary/// author:Stone_W/// date:2010.12.1/// desc:缓存的管理类/// 注意要添加对引用 System.Web/// /summarypublic class MyCacheTools : System.Web.SessionState.IRequiresSessionState{ #region 存入Cache /// summary /// 存入Cache /// /summary /// param namekey缓存key/param /// param namevalue缓存的值/param /// param nametime_HH存xx小时/param /// returns是否执行成功[bool]/returns public static bool SetCache(string key, object value, int time_HH) { bool result false; try { DateTime dt DateTime.Now.AddHours(time_HH); System.Web.HttpRuntime.Cache.Insert(key, value, null, dt, System.Web.Caching.Cache.NoSlidingExpiration); result true; } catch (Exception ex) { } return result; } #endregion #region 取得Cache /// summary /// 取得Cache /// /summary /// param namekeykey/param /// returnsobject类型/returns public static object GetCache(string key) { return System.Web.HttpRuntime.Cache.Get(key); } #endregion #region 查询Cache是否存在 /// summary /// 查询Cache是否存在 /// /summary /// param namekeykey 值/param /// returnsbool/returns public static bool IsCacheExist(string key) { bool result false; object temp System.Web.HttpRuntime.Cache.Get(key); if (null ! temp) { result true; } return result; } #endregion} ps:HttpContext.Current.Cache 为null 这个问题搞的我很痛苦最后还是解决了希望此篇文章对大家有用