Session.Abandon();//取消当前会话
Session.Clear();//清空值

Asp.net有三种方式存储会话状态信息:
    1. 存储在进程中: 属性mode = InProc
       特点:  具有最佳的性能,速度最快,但不能跨多台服务器存储共享.
 
    2. 存储在状态服务器中: 属性mode = "StateServer"
       特点:   当需要跨服务器维护用户会话信息时,使用此方法。
               但是信息存储在状态服务器上,一旦状态服务器出现故障,信息将丢失
   
    3. 存储在Sql Server中: 属性mode="SqlServer"
       特点:   工作负载会变大,但信息不会丢失.

I. 由于某些页面不需要会话状态,则可以将会话状态禁用:
             代码如下: <%@ Page EnableSessionState="false" %>
       II.如果页面需要访问会话变量但不允许修改它们,可以设置页面会话状态为只读:
             代码如下: <%@ Page EnableSessionState="false" %>

二).使用Page.IsPostBack
    Page.IsPostBack表示是否是从客户端返回的. 初次运行时,不是从客户端返回,它的值
    为false,当触发页面上的事件或刷新页面时,Page.IsPostBack由于是回发的,值变为true;
   

六).缓存优化
     缓存分为两种: 页面缓存和API缓存.
1.使用页面缓存和片段缓存 
        <%@ OutputCache Duration="5" VaryByParam="None"%> 
        <%@ OutputCache Duration=60 VaryByParam=”TextBox1,TextBox2” %>
      说明: Duration是设置Cache的过期时间;
          VarByParam是设置是否根据参数而变化,None时所有参数使用同一Cache, 
          设置TextBox1时则根据TextBox1的不同值分别缓存;当有多个参数时则要组合缓存;

/// <head>
///  <function>
///   存储类(存储UserInfo信息)
///  </function>
///  <description>
///   用Cache存储用户信息
///   在指定间隔(TimeOut)内取,则可以从Cache中取,
///   如果超出存储时间,则从数据库取用户信息数据
///   作為所有用户信息的存儲類.
///  </description>
///  <author>
///   <name>ChengKing</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
 }
}

 II.使用时注意Page.Cache和HttpContext.Current.Cache区别:
          它们指的同一个对象,在Page里,用Page.Cache,如果在global.asax或自己的类里用:HttpContext.Current.Cache
          在有些事件中,由于其没有HttpContext,就用HttpRuntime.Cache.

 posted on 2011-04-16 12:54  志神Soft  阅读(171)  评论(0)    收藏  举报