控制缓存在asp.net中

时间:2009-10-25 09:21:23

标签: asp.net caching

在一个模块中,我有一个创建modalpopup并显示动作结果的子。我不想放弃&拖动modalpopup扩展程序并使html复杂,因此我在代码隐藏中这样做。我想知道我可以缓存我的面板然后从缓存中获取吗?需要时间来创建这些面板。或者你有推荐来改善这个Sub的性能?我使用Script Combining,JS minify和Css minify.My sub的代码在这里。

Public Sub Raise_Alarm(ByVal p_Page As Page, ByRef p_AssignedButton As System.Web.UI.WebControls.Button, ByVal p_Message As String, Optional ByVal p_Type As MessageType = MessageType.Success)

    Dim placeHolder As PlaceHolder = p_Page.FindControl("plcMsg")

    Dim lblStatus As System.Web.UI.WebControls.Label
    lblStatus = p_Page.FindControl("lblStatus")
    lblStatus.Width = Unit.Percentage(100)
    lblStatus.Font.Name = "verdana"
    lblStatus.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall


    'Create Modalpopup extender
    Dim mdlMessage As New AjaxControlToolkit.ModalPopupExtender

    'Create Divs
    Dim pnlMessage As New System.Web.UI.WebControls.Panel
    Dim pnlFrame As New System.Web.UI.WebControls.Panel
    Dim pnlContainer As New System.Web.UI.WebControls.Panel
    Dim pnlHeader As New System.Web.UI.WebControls.Panel
    Dim pnlMsg As New System.Web.UI.WebControls.Panel
    Dim pnlBody As New System.Web.UI.WebControls.Panel
    Dim pnlFooter As New System.Web.UI.WebControls.Panel
    Dim pnlRight As New System.Web.UI.WebControls.Panel
    '*******

    'Create Ok Buttons

    Dim btnOk As New System.Web.UI.WebControls.Button

    'Create Message Label

    Dim lblMessage As New System.Web.UI.WebControls.Label
    Dim myheader As New System.Web.UI.WebControls.Label
    'Assign Properties 

    pnlMessage.ID = "pnlMessage"
    pnlMessage.CssClass = "modal-dialog"
    pnlMessage.Style.Add("display", "none")

    pnlFrame.ID = "pnlframe"
    pnlFrame.CssClass = "frame"
    pnlContainer.ID = "container"
    pnlContainer.CssClass = "container"
    pnlHeader.ID = "header"
    pnlHeader.CssClass = "header"

    myheader.ID = "headerlabel"
    myheader.Text = "Warning"

    pnlMsg.ID = "msg"
    pnlMsg.CssClass = "msg"

    pnlBody.ID = "body"
    pnlBody.CssClass = "body"

    pnlFooter.ID = "footer"
    pnlFooter.CssClass = "footer"

    pnlRight.ID = "right"
    pnlRight.CssClass = "right"

    btnOk.ID = "btnOk"
    btnOk.Width = Unit.Pixel(50)
    btnOk.Text = "Ok"
    btnOk.CssClass = "but"      
    btnOk.OnClientClick = "Dispose()"



    lblMessage.ID = "lblMessage"
    lblMessage.Text = p_Message
    lblMessage.CssClass = "body"

    If p_Type = MessageType.Success Then

        lblStatus.ForeColor = System.Drawing.Color.Blue
        lblStatus.Text = p_Message
        lblStatus.Visible = True

    ElseIf p_Type = MessageType.Error Then

        lblStatus.ForeColor = System.Drawing.Color.Red

    End If


    pnlRight.Controls.Add(btnOk)
    pnlFooter.Controls.Add(pnlRight)
    pnlBody.Controls.Add(lblMessage)
    pnlBody.Controls.Add(pnlFooter)
    pnlMsg.Controls.Add(myheader)
    pnlHeader.Controls.Add(pnlMsg)
    pnlContainer.Controls.Add(pnlHeader)
    pnlContainer.Controls.Add(pnlBody)
    pnlFrame.Controls.Add(pnlContainer)
    pnlMessage.Controls.Add(pnlFrame)


    With mdlMessage


        .TargetControlID = p_AssigedButton.ID
        .PopupControlID = pnlMessage.ID
        .ID = "mdlMessage"
        .BackgroundCssClass = "modalBackground"
        .OkControlID = btnOk.ID
        .Page = p_Page

    End With

    placeHolder.Controls.Add(pnlMessage)
    placeHolder.Controls.Add(mdlMessage)
    mdlMessage.Show()
End Sub  

2 个答案:

答案 0 :(得分:1)

你有没有测量过这个子的表现?

我们看过这一次,发现甚至1000个创建对象仍然只有几纳秒。

如果要缓存某些东西,它必须是可序列化的。为了给你一个改进,它必须花费更少的时间来反序列化,而不是创建一个新的新对象所需的时间。

看起来你没有从数据库中获取任何值,所以我认为你不能用缓存来优化这段代码。

修改

还有其他几个原因没有进行缓存:

  • 使程序复杂化,您需要先检查某个值是否在缓存中,然后再使用它。
  • 有可能引入错误。以lblStatus为例,这会根据参数更改值。如果您只是从缓存中读取它,则可能会得到一个与您的参数不匹配的文件。

答案 1 :(得分:0)

将控件插入缓存中:

//Make sure to adjust the expiration of the cache with a contstant
Cache.Insert("pnlPanelMessage", pnlMessage, null, DateTime.MaxValue, TimeSpan.FromMinutes(15));

从缓存中检索控件:

System.Web.UI.WebControls.Panel pnlMessage = (System.Web.UI.WebControls.Panel)Cache.Get("pnlPanelMessage")

MSDN:ASP.NET Caching: Techniques and Best Practices