ASP.net How to display webusercontrol cache on public properties controls

I have web user control, it handles some potentially intensive data calculations, and I would like it to be displayed in the cache so that every page view does not recount the data. It is located on very frequently viewed pages, so it is very important that I work correctly!

For context, it was used in our arcade: http://www.scirra.com/arcade/action/93/8-bits-runner

Click on statistics, data for graphs and statistics are created from this webusercontrol.

The start of management is as follows:

public partial class Controls_Arcade_Data_ArcadeChartData : System.Web.UI.UserControl
{
    public int GameID { get; set; }
    public Arcade.ChartDataType.ChartType Type { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

Now the difficulty that I am facing is that the output cache must depend on both GamID and ChartType.

GameID , , , .

, GameID = 93 Type = GraphData, GameID = 41 Type = TotalPlaysData, GameID = 93, Type = TotalPlaysData. .

( )

<div>Total Plays:</div>
<div class="count"><Scirra:ArcadeChartData runat="server" GameID="93" Type="TotalPlays" /></div>
<br /><br />
<div>Total Guest Plays:</div>
<div class="count"><Scirra:ArcadeChartData runat="server" GameID="93" Type="TotalGuestPlays" /></div>

etc.

! , -, , , .

Edit

: :   <% @OutputCache Duration = "20" VaryByControl = "GameID; " % >

Object reference not set to an instance of an object. , GameID ASPX .

+5
8

, , ; , , . , , . ( ExpressionBuilder , ).

, , null:

if (this.YourControlID != null) // true if not from cache
{
    // do stuff
}

, .

:

<%@ OutputCache Duration="20" VaryByControl="GameID;Type" Shared="true" %>

, . Shared="true" . Shared="true" - , - , .

, , , . , Control . , .

OutputCache :

[PartialCaching(20, null, "GameID;Type", null, true)]
public partial class Controls_Arcade_Data_ArcadeChartData : UserControl
+9

:

1) :

<%@ OutputCache Duration="21600" VaryByParam="None" VaryByCustom="FullURL" %>

2) global.asax :

    public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        if (arg.Equals("FullURL", StringComparison.InvariantCultureIgnoreCase)
        {
            // Retrieves the page
            Page oPage = context.Handler as Page;

            int gameId;

            // If the GameID is not in the page, you can use the Controls 
            // collection of the page to find the specific usercontrol and 
            // extract the GameID from that.

            // Otherwise, get the GameID from the page

            // You could also cast above
            gameId = (MyGamePage)oPage.GameID;

            // Generate a unique cache string based on the GameID
            return "GameID" + gameId.ToString();
        }
        else
        {
            return string.Empty;
        }
    }

GetVaryByCustomString MSDN, .

+3

HttpRuntime.Cache.Insert("ArcadeChartData" + GameID + Type, <object to cache>, null, System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, secondsToCache),CacheItemPriority.Normal, null);

, , ,

Response.AddCacheItemDependency("ArcadeChartData" + GameID + Type);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

, SetExpires SetCacheability Cache.

+2

, , , , , . UserControl.

<%@ OutputCache Duration="10" VaryByParam="none" %>

. Framework 4.0. , UserControl (MyInt, My String ), Page_Init.

:

:

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="MyWebApp._Default" %>
<%@ Register Src="~/UserControl/MyUserControl.ascx" TagPrefix="uc" TagName="MyUserControl" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <uc:MyUserControl ID="uc1" MyInt="1" MyString="Test"  runat="server" />
    <hr />
    <uc:MyUserControl ID="uc2" MyInt="3" MyString="Test"  runat="server" />
    <hr />      
    <uc:MyUserControl ID="uc3" MyInt="1" MyString="Testing" runat="server" />
</asp:Content>

:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyUserControl.ascx.vb" Inherits="MyWebApp.MyUserControl" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>

<div style="background-color:Red;">
    Test<br />
    <asp:Label ID="lblTime" ForeColor="White" runat="server" />
</div>

:

Public Class MyUserControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Debug.Write("Page_Load of {0}", Me.ID)
        Dim oStrBldr As New StringBuilder()
        For i As Integer = 1 To Me.MyInt
            oStrBldr.AppendFormat("{0}: {1} - {2} at {3}<br />{4}", Me.ID, i, Me.MyString, Date.Now.ToLongTimeString(), System.Environment.NewLine)
        Next
        Me.lblTime.Text = oStrBldr.ToString()
    End Sub


    Public Property MyInt As Integer
    Public Property MyString As String

End Class

, , , , . #

+1

, , GameId Type, , iframe . - .

0

, , OutputCache , Propertys, QueryString. , , , , , , .

QueryString, UserControl -, UserControl, iframe, UserControl QueryString.

UserControl:

<iframe src="/MyArcadeChartData.aspx?GameID=93&Type=TotalPlays"></iframe>

, MyArcadeChartData.aspx

<%@ Page ... %>
<%@ OutputCache Duration="20" VaryByParam="GameID;Type" %>
<Scirra:ArcadeChartData ID="MyUserControlID" runat="server />

, MyArcadeChartData.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    //TODO: Put validation here
    MyUserControlID.GameID = (int)Request.QueryString["GameID"];
    MyUserControlID.Type = (YourEnum)Request.QueryString["Type"];
}

, , QueryString , .

Im , , , , .

0

, - , , , , , , , .

HttpHandlerFactory, , , ( ), . . , , .

0

If this is not data intensity, do you consider storing it in a session, how was it done for caching? Just a thought ...

Arcade.ChartDataType.ChartType Type;
string GameKey = GameId + Type.toString();
storedData = callCalculation(GameId,Type);
Session[GameKey] = storedData;

I understand that this is not in the cache, I'm just trying to be constructive.

0
source

All Articles