使用缓存

12 篇文章 0 订阅

一、           设置缓存过期失效

可以调用CacheAdd方法来建立一个Cache项,在该方法的实参中可以指定过期失效,依赖项和指定回调函数等。Add方法的原型为:

public Add(

string key,

object value,

CacheDependency dependencies,

DateTime absoluteExpiration,

TimeSpan slidingExpiration,

CacheItemPriority priority,

CacheItemRemovedCallback onRemoveCallback);

其中:

n        key 该引用项的键。

n        value该引用项的值。以上两项构成Cache的一对“键值”。

n        dependencies 文件依赖项或键依赖项。当任何以来项发生更改时,该对象即无效,并从缓存中移除。如果不需要依赖项,该参数指定为空引用。

n        absoluteExpiration 该项过期失效的绝对时间。

n        slidingExpiration该项过期失效的相对时间,即相对与最后一次访问该项的时间间隔。

n        priority 移除缓存的优先级,由CacheItemPriority枚举量表示。缓存在退出对象时使用该值,具有较低成本的对象在具有较高成本的对象之前移除。

n        onRemoveCallback 在从缓存中移除对象是调用的委托。在移除对象时需要执行的代码(完成某些功能)写在该委托实例中。若不需要回调,该参数指定为空引用。

n        返回值 Cache项的值。

注意:不可以同时指定到期的绝对时间和相对时间间隔。若要指定到期的绝对时间,请将相对时间间隔指定为TimeSpan.Zero;若要指定相对时间间隔,请将绝对时间指定为DateTime.MaxValue

以下是一个使用缓存过期的例子。

[WebMethod]

public DateTime[] CacheDateTime()

{

     DateTime []ret=new DateTime[2];

     //从缓存中取结果

     if(Context.Cache["Date"]==null)

     {

         //创建时间间隔实例,间隔1分钟

         TimeSpan t=new TimeSpan(0,1,0);

         //调用Add方法加入Cache--值对

         Context.Cache.Add("Date",                 //

                        DateTime.Now,            //

                          null,                    //无依赖项

                          DateTime.MaxValue,       //不指定绝对时间

                          t,                            //相对时间间隔

                          CacheItemPriority.High,       //移除策略

                          null                          //不回调

                       );

          }

     ret[0]=(DateTime)Context.Cache["Date"];

     ret[1]=DateTime.Now;

     return ret;

}

要编译该程序,需要在头部加入 using System.Web. Caching;

第二个例子:

[WebMethod]

public DataSet GetAllInfoFromCache()

{

     if(Context.Cache["DataSet"]==null)

     {

         DataSet mySet=new DataSet();

         //创建时间间隔实例,间隔1分钟

         TimeSpan t=new TimeSpan(0,1,0);

         //调用Add方法加入Cache--值对

         Context.Cache.Add("DataSet",                   //

              mySet,                                     //

              null,                       //无依赖项

              DateTime.MaxValue,     //不指定绝对时间

              t,                          //相对时间间隔

              CacheItemPriority.High,       //移除策略

              null                        //不回调

              );

         }

              return (DataSet)Context.Cache["DataSet"];

     }

二、           使用回调

若定义了回调委托的实例,就可以在Cache项失效时回调该委托实例。将需要在Cache项失效时完成的某些功能代码写在该委托实例中,以便调用。

要实现回调,就要编写一个符合下面签名格式的委托。

[Serializable]

public delegate void CacheItemRemovedCallback(

string key,

object value,

CacheItemRemavedReason reason)

其中参数:

n        key 被移除的缓存项的键名。

n        value被移除的缓存项的键值。

n        reason 移除该缓存项的原因,该参数的取值为CacheItemRemavedReason枚举量。

实例:

[Serializable]

public delegate void CacheItemRemovedCallback(string key,object value,CacheItemRemovedReason reason);

void OnRemove(string key,object value,CacheItemRemovedReason reason)

{

     string fileName;

     fileName=Server.MapPath(null)+"//log.txt";

     FileStream fs=File.Open(fileName,FileMode.CreateNew);

     StreamWriter write=new StreamWriter(fs);

     string msg;

     msg=String.Format("键名:{0},键值:{1},失效原因:{2}/n",key,value,reason);

     msg+=String.Format("失效时间:{0}/n",DateTime.Now);

     write.WriteLine(msg);

     write.Close();

     fs.Close();

}

 [WebMethod]

public DateTime[] CacheCallback()

{

     DateTime []ret=new DateTime[2];

     //从缓存中取结果

     if(Context.Cache["Date"]==null)

     {

         //创建时间间隔实例,间隔1分钟

         TimeSpan t=new TimeSpan(0,1,0);

         //创建回调实例

         System.Web.Caching.CacheItemRemovedCallback myCallBack=new System.Web.Caching.CacheItemRemovedCallback(OnRemove);

         //实例化文件依赖

         string fileName="E://SQL Server//MSSQL//Data//hygl_Data.MDF";

         CacheDependency cd=new CacheDependency(fileName);

         //调用Add方法加入Cache--值对

         Context.Cache.Add("Date",   //

              DateTime.Now,          //

              cd,                        //依赖项

              DateTime.MaxValue,     //不指定绝对时间

              t,                          //相对时间间隔

              CacheItemPriority.High,  //移除策略

              myCallBack                  //回调

              );

     }

     ret[0]=(DateTime)Context.Cache["Date"];

     ret[1]=DateTime.Now;

     return ret;

}

三、           设置服务方法的CacheDuration属性

通过在Web服务的方法上这种CacheDuration属性,可以强制将方法的返回值保存一段时间,在此时间间隔内,方法的返回值总是从Cache中取得的。

以下实例将Web服务方法的结果保存在Cache60秒。

[WebMethod(CacheDuration=60)]

public DateTime GetDateTime()

{

     return DateTime.Now;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值