ASP.NETキャッシュのWebサーバキャッシュ

4671 ワード

1.キャッシュの置換
substitutionを使用してキャッシュ・ページを動的に更新できます.
    public static string GetTime(HttpContext context)
    {
        return DateTime.Now.ToString();
    }

aspx
  Cached Time:
    
Page Time:

最後にページでキャッシュ出力を開く
結果:Page Timeは毎回異なり、cached Timeは5秒ごとに更新されます
Tips:キャッシュ出力を閉じる
this.Response.Cache.SetNoServerCaching();

2.データ依存
データベースに依存するページを、対応するsql文に関連付けてキャッシュする方法を設定します.


, DML , , , .

, SqlDependency, AddCacheDependency :

 using (SqlConnection conn = new SqlConnection(connectionString))
        {
            string sql = @"SELECT U.UserId,
	                            U.UserName,
	                            D.BorrowDate,
	                            D.BorrowMoney,
	                            D.RevertDate,
	                            D.IsRevert
                            FROM dbo.UserInfo AS U
                            INNER JOIN dbo.Debt AS D
	                            ON U.UserId = D.UserId";
            conn.Open();
            SqlCommand sqlCmd = new SqlCommand(sql,conn);
            SqlCacheDependency dep = new SqlCacheDependency(sqlCmd);


            GridView1.DataSource = sqlCmd.ExecuteReader();
            GridView1.DataBind();
            this.Response.AddCacheDependency(dep);
            
        }

Global.asax :

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        System.Data.SqlClient.SqlDependency.Start(ConfigurationManager.AppSettings["DebtDB"].ToString());

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        System.Data.SqlClient.SqlDependency.Stop(ConfigurationManager.AppSettings["DebtDB"].ToString());
    }
        

: , , DML , , ,


, ,

3. varyByCustom

, Cookie, .

  
cache time: userId:


:userId

 


 

    protected string userId;
    public readonly string connectionString = ConfigurationManager.AppSettings["DebtDB"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["userId"] != null)
        {
             userId = Request.QueryString["userId"].ToString();
        }

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            string sql = @"
                            SELECT U.UserId,
	                            U.UserName,
	                            D.BorrowDate,
	                            D.BorrowMoney,
	                            D.RevertDate,
	                            D.IsRevert
                            FROM dbo.UserInfo AS U
                            INNER JOIN dbo.Debt AS D
	                            ON U.UserId = D.UserId
                                WHERE D.UserId =" + userId;
            conn.Open();
            SqlCommand sqlCmd = new SqlCommand(sql, conn);
            GridView1.DataSource = sqlCmd.ExecuteReader();
            GridView1.DataBind();

        }
    }


Global.asax

GetVaryByCustomString

UserId 。

UserId:1 , Userid:2 , , userid:2

 

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom == "userId")
        {
            return context.Request.QueryString["userId"].ToString();
        }
        return base.GetVaryByCustomString(context, custom);
    }