您的位置:首页 > 编程语言 > ASP

Asp.Net中Cache操作类

2011-01-20 18:00 387 查看
asp.net cache 简述_asp.net技巧
Asp.net支持三种类型的cache 想写一个技术快速概述,可能写得太多了。技术概略的目的是以最快最简单的方式描述出技术要点,也是我希望的最有效率的知识传播方式。

1.  页面/控件cache

2.  应用程式级cache

3.  浏览器客户端cache

从实现方式来看,页面/控件cache和应用程式级cache都是存放在服务器内存里面的,随着内存的紧张程度,这些内容有可能在失效之前被提前删除。(cache的特性决定这些内容是能放心得删除掉的)。浏览器客户端的cache是存放在客户端浏览器的cache里面 ,比如IE的临时目录就是起的cache的作用。每次用户请求一个页面的时候,浏览器会先从cache里面去查找一下有没有符合需求的还没有过期的cache内容,如果有的话就从cache里面直接读取跳过网络传输。

下面演示一下在Asp.net里面具体的写法:

1.       页面/控件cache能申明在aspx,ascx文件里面,也能在code behind里面作出申明。

<%@ OutputCache Duration="#ofseconds"

   Location="Any | Client | Downstream | Server | None |

     ServerAndClient "

   Shared="True | False"

   VaryByControl="controlname"

   VaryByCustom="browser | customstring"

   VaryByHeader="headers"

   VaryByParam="parametername"

   CacheProfile="cache profile name | "

   NoStore="true | false"

   SqlDependency="database/table name pair | CommandNotification"

%>

主要的参数是:

Duration: cache有效的时间,单位秒

Shared:只对控件有效,在控件能同时应用和多个页面的时候,多个页面是否能共享这一个cache而不用每个页面维护自己对这个控件的cache。

VaryByControl:cache随控件的ID变化

VaryByCustom:cache随用户自定义的一个变量来变化,该变量是在这里指定,然后应该在Global.ascx里面实现:

public override string GetVaryByCustomString (

HttpContext context,

string custom

)

该声明的custom参数就是在OutPutcache里面申明的变量。通过在这个函数里面对不同的custom参数返回不同的字符串值来表示不同的cache内容。

VaryByHeader, VaryByParam,CacheProfile也是设置不同的cache版本的。

NoStore表示不允许把cache的内容写到内存以外其他的存储设备上,这是对安全性比较高的内容的需要。

SqlDependency是和数据库相关的。

2.       应用程式级cache

只能在code behind里面获得,特点是能实现自己需要的所有cache逻辑。

能通过Page.Cache获得,类的名字是System.Web.Caching.Cache

3.       浏览器客户端cache

只能在codebehind里面获得,能通过Response.Cache来指定,这是个HttpCachePolicy对象。

另外: 能指定一个TimeSpan作为cache的时间,不用每次来折算到秒。

        public TimeSpan(long ticks);       

        public TimeSpan(int hours, int minutes, int seconds);

        public TimeSpan(int days, int hours, int minutes, int seconds);

        public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);

如果要cache保留一个小时1分1秒,能直接

new TimeSpan(1,1,1)

而不用先计算

1小时1分1秒=3600+60+1=3661秒,然后在传入3661作为有效期

 

Asp.Net中Cache操作类

/// <head>
///  <function>
///   存储类(存储UserInfo信息)
///  </function>
///  <description>
///   用Cache存储用户信息
///   在指定间隔(TimeOut)内取,则可以从Cache中取,
///   如果超出存储时间,则从数据库取用户信息数据
///   作為所有用户信息的存儲類.
///  </description>
///  <author>
///   <name>L</name>   
///  </author>
/// </head>
using System;
using System.Web;
using System.Web.Caching;

namespace Common
{     
        /// <summary>
/// 存储类(存储UserInfo信息)
/// </summary>
public class Storage
{
  public Storage()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  #region 方法

  //实现“一键一值”存储方法,最普通的存储方法  
                //(“一键一值”指一个Identify存储一个值,下面还有一个“一键多值”方法,因为有时候需要一个键存储多个变量对象值)
                public static bool InsertIdentify(string strIdentify,object Info)
  {   
   if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)
   {
    //建立回调委托的一个实例
    CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
    //以Identify为标志,将userInfo存入Cache
    HttpContext.Current.Cache.Insert(strIdentify,userInfo,null,
    System.DateTime.Now.AddSeconds(300),
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.Default,
    callBack);
    return true;
   }   
   else
   {
    return false;
   }
  }
  //判断存储的"一键一值"值是否还存在(有没有过期失效或从来都未存储过)
                public static bool ExistIdentify(string strIdentify)
  {
   if(HttpContext.Current.Cache[strIdentify] != null)
   {
    return true;
   }
   else
   {
    return false;
   }
  }

                //插入"一键多值"方法
                //***其中 StorageInfType是一个Enum,里面存有三种类型: UserInf SysInf PageInf
                //这个枚举如下:
                /*
                  public enum StorageInfType
            {
        /// <summary>用户信息</summary>
            UserInf = 0,
        /// <summary>页面信息</summary>
        PageInf = 1,  
        /// <summary>系统信息</summary>
               SysInf = 2
              }
                //此枚举是自己定义的.可根据需要定义不同的枚举 
                //加个枚举目的是实现“一键多值”存储方法,事实上Cache中是存放了多个变量的,只不过被这个类封装了,
                //程序员感到就好像是“一键一值”.   这样做目的是可以简化开发操作,否则程序员要存储几个变量就得定义几个Identify.
  public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
  {   
   if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)
   {
    //RemoveCommonInf(strIdentify,enumInfType); 
    //建立回调委托的一个实例
            CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);

    if(enumInfType == StorageInfType.UserInf)
    {    
       //以用户UserID+信息标志(StorageInfType枚举),将userInfo存入Cache
        HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.UserInf.ToString(),objValue,null,
       System.DateTime.Now.AddSeconds(18000),       //单位秒
       System.Web.Caching.Cache.NoSlidingExpiration,
       System.Web.Caching.CacheItemPriority.Default,
       callBack);  
    }
    if(enumInfType == StorageInfType.PageInf)
    {
     //以用户UserID+信息标志(StorageInfType枚举),将PageInfo存入Cache
         HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.PageInf.ToString(),objValue,null,
         System.DateTime.Now.AddSeconds(18000),
         System.Web.Caching.Cache.NoSlidingExpiration,
         System.Web.Caching.CacheItemPriority.Default,
         callBack);  
    }
    if(enumInfType == StorageInfType.SysInf)
    {
     //以用户UserID+信息标志(StorageInfType枚举),将SysInfo存入Cache
         HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.SysInf.ToString(),objValue,null,
         System.DateTime.Now.AddSeconds(18000),
            System.Web.Caching.Cache.NoSlidingExpiration,
         System.Web.Caching.CacheItemPriority.Default,
         callBack);  
    }
    return true;
   }
   return false;
  }
                //读取“一键多值”Identify的值
                public static bool ReadIdentify(string strIdentify,out UserInfo userInfo)
  {
   //取出值
   if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
   {
    userInfo = (UserInfo)HttpContext.Current.Cache[strIdentify];
    if(userInfo == null)
    {
     return false;
    }
    return true;
   }   
   else
   {
    userInfo = null;
    return false;
   }   
  }

  //手动移除“一键一值”对应的值
                public static bool RemoveIdentify(string strIdentify)
  {
   //取出值
   if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
   {
    HttpContext.Current.Cache.Remove(strIdentify);        
   }   
   return true;  
  }

                //此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据
  private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
  {
  }

  #endregion

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: