添加 Cache[Key]=object or Cache.Insert
移除 Cache.Remove(key)
1、将值直接写入Cache
代码如下 复制代码 HttpContext.Current.Cache["One"] = "1";
使用'绝对过期'方式处理缓存,过期时间为:9999年12月31日 (不推荐使用该方法处理缓存,并且应在适当的时候清空缓存Key)
2、使用Insert(String, Object)插入Cache
代码如下 复制代码 string cacheKey = "Two";object cacheValue = HttpContext.Current.Cache.Get(cacheKey);
if(cacheValue == null){ cacheValue = WebConfigurationManager.ConnectionStrings["applicationServices"].ConnectionString;
HttpContext.Current.Cache.Insert(cacheKey, cacheValue);}
//显示指定缓存的Key 与 Valuethis.ShowMessage(cacheKey, cacheValue.ToString());
3、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan)插入Cache
代码如下 复制代码 string cacheKey = "__cache__students";
DataSet dataSet = this.Cache.Get(cacheKey) as DataSet;
if(dataSet == null){ dataSet = new DataSet();
//加载xml并填充至DataSet dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));
//加入缓存,并设定'绝对过期时间'为5分钟 this.Cache.Insert(cacheKey, dataSet, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);}
//绑定DataGrid数据grdDefault.DataSource = dataSet;grdDefault.DataBind();
该方法较重要的两个参数为absoluteExpiration及slidingExpirationabsoluteExpiration DateTime类型,代表绝对过期时间slidingExpiration TimeSpan类型,代表滑动过期时间absoluteExpiration与slidingExpiration不能同时使用例如:设定了absoluteExpiration参数时,slidingExpiration必须设定为System.Web.Caching.Cache.NoSlidingExpiration反之:设定了slidingExpiration参数时,absoluteExpiration必须设定为System.Web.Caching.Cache.NoAbsoluteExpiration
4、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPRiority,
更多详细内容请查看:http://www.111cn.net/net/net/56762.htm
CacheItemRemovedCallback)插入Cache
public partial class PriorityAndCallbackDemo : System.Web.UI.Page{ #region 静态字段 static bool CacheItemRemoved = false; static CacheItemRemovedReason Reason; static string CacheItemKey = "fw__cache__students"; #endregion
#region 事件处理 //页面加载 protected void Page_Load(object sender, EventArgs e) { //缓存项已移除 if(PriorityAndCallbackDemo.CacheItemRemoved) { ltMessage.Text = string.Format("Key={0}已从缓存移出,原因为:{1}", PriorityAndCallbackDemo.CacheItemKey, PriorityAndCallbackDemo.Reason.ToString()); } }
//'添加缓存'按钮 点击事件 处理 protected void btnAddCache_Click(object sender, EventArgs e) { DataSet dataSet = this.Cache.Get(PriorityAndCallbackDemo.CacheItemKey) as DataSet;
if(dataSet == null) { dataSet = new DataSet(); dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));
//使用 Web.config 作为缓存过期依赖项 CacheDependency dependency = new CacheDependency(this.Server.MapPath(@"Web.config"), DateTime.Now);
//加入缓存,设定优先级为默认级别 this.Cache.Insert(PriorityAndCallbackDemo.CacheItemKey, dataSet, dependency, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(this.CacheItemRemovedHandler)); }
//绑定GridView数据 grdDefault.DataSource = dataSet; grdDefault.DataBind(); }
//'移除缓存'按钮 点击事件 处理 protected void btnRemoveCache_Click(object sender, EventArgs e) { if(this.Cache[PriorityAndCallbackDemo.CacheItemKey] != null) { this.Cache.Remove(PriorityAndCallbackDemo.CacheItemKey); } } #endregion
#region 私有方法 //缓存项移除事件处理 private void CacheItemRemovedHandler(string key, object value, CacheItemRemovedReason relason) { PriorityAndCallbackDemo.CacheItemRemoved = true; PriorityAndCallbackDemo.Reason = relason; } #endregion}
该方法较重要的两个参数为CacheItemPriority及CacheItemRemovedCallbackCacheItemPriority 缓存项优先级,当服务器内存不够时,优先级越高的项越不容易被移除CacheItemRemovedCallback 该参数为委托类型,当缓存项被移除时所调用,包含Reason参数用于表示缓存项被移除的原因
【我是怎么用的】
首先理解缓存策略。可调过期策略 和 绝对过期策略。注意,两则不能同时使用
使用可调过期策略,需要将absoluteExpiration=DateTime.MaxValue ,TimeSpan .FromMinutes(10)设置项目只有在10分钟内不被使用才会被移除
代码如下 复制代码 Cache.Insert("data", "123", null , DateTime.MaxValue, TimeSpan.FromMinutes(10));
绝对策略,如天气报告,将信息保存60分钟
代码如下 复制代码 Cache.Insert("data", "123", null , DateTime.Now.AddMinutes(60), TimeSpan.Zero);
缓存依赖。
即一个缓存的失效依赖另外一个object。这里的object可以指另外一个缓存,或者一个文件,或者....
类:CacheDependency 命名空间 System.Web.Caching.CacheDependency依赖于其它缓存项目
代码如下 复制代码 System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency (null, new string [] { "time" });Cache.Insert( "number", ++num, cacheDependency);依赖于文件或文件夹System.Web.Caching. CacheDependency cacheDependency = new System.Web.Caching.CacheDependency ( "test.xml");当test.xml文件删除、更新时自动从缓存中移除System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency(null, new string[] { "time" });Cache.Insert("test", "123", cacheDependency);
移除项目回调Cache.Insert("test", "123", null , DateTime.Now.AddSeconds(10), TimeSpan.Zero, new CacheItemUpdateCallback(Test));private void Test(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration) { }
新闻热点
疑难解答