在 ASP.NET 中使用 PartialCaching 因控件属性而异 [英] Vary by control properties using PartialCaching in ASP.NET

查看:17
本文介绍了在 ASP.NET 中使用 PartialCaching 因控件属性而异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户控件的基类上使用 PartialCaching 属性.

I am using the PartialCaching attribute on the base class of a user control.

我希望缓存的控件根据控件实例上设置的属性而有所不同.

I would like the cached controls to vary based on the properties set on the control instance.

例如:

<mycontrols:control1 runat="server" param1="10" param2="20" />

...输出将与具有不同属性的控件实例分开缓存:

...output would be cached separately from a control instance with different properties:

<mycontrols:control1 runat="server" param1="15" param2="20" />

...而且这个控件也会被单独缓存:

...and this control would be cached separately as well:

<mycontrols:control1 runat="server" param1="10" param2="25" />

但是,如果两个单独页面上的两个控件实例具有相同 param1 和 param2 属性,我希望它们作为一个对象缓存(以便共享缓存的控件).

However, if two control instances on two separate pages had identical param1 and param2 properties, I'd like them to cache as one object (so that cached control would be shared).

以上用例可以通过 PartialCaching 属性实现吗?我会使用哪些设置?VaryByControl?

Can the above use case be achieved with PartialCaching attribute? What settings would I use? varyByControl?

另外,是否可以在运行时使缓存持续时间可变?

Also, is it possible to make the cache duration variable at runtime?

谢谢.

推荐答案

要回答你的第一个 Q,让我先告诉你,你的问题本身就有答案;).'Shared' ... 是的,这就是关键字 :) 要在缓存中为所有页面的用户控件创建一个实例,请在 @OutputCache 指令中设置 Shared='true'.这应该在用户控制级别设置,即在 ascx 页面中.

To answer your first Q, let me first tell you that your question itself has the answer ;). 'Shared' ... yes that's the keyword :) To have a single instance in cache for the user control across all the pages, set Shared='true' in the @OutputCache directive. This should be set at the user control level i.e. in the ascx page.

要根据用户控件属性缓存用户控件,您应该在 PartialCachingAttribute 的varyByControls 部分中指定属性的完全限定名称.多个属性(如果有)应该用分号分隔.

To cache the user control based on user control properties, you should specify the fully qualified name of the properties in the varyByControls section of the PartialCachingAttribute. Multiple properties if any should be separated by semi-colons.

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" 
Inherits="UC_WebUserControl" %>
<%@ OutputCache Duration="60" 
VaryByControl="UC_WebUserControl.param1;UC_WebUserControl.param2" 
VaryByParam="none" Shared="true" %>

或者您也可以为用户控件包含 PartialCache 属性:

or you can also include the PartialCache attribute for the user control:

[PartialCaching(60, null, "UC_WebUserControl.param1;UC_WebUserControl.param2", null, true)]
public partial class UC_WebUserControl : System.Web.UI.UserControl
{
    public string param1 { get; set; }
    public string param2 { get; set; }

}

或者另一种在两个值的组合上缓存控件的方法是:

OR another way to cache the control on the combination of both values would be:

[PartialCaching(60, null, "UC_WebUserControl.BothParams", null, true)]
public partial class UC_WebUserControl : System.Web.UI.UserControl
{
    public string param1 { get; set; }
    public string param2 { get; set; }

    public string BothParams    
    {
        get { return String.Concat(param1, param2); }
    }

}

最后一个参数 (true) 指定共享.持续时间由 60 指定.请参阅链接 如何:缓存用户的多个版本基于参数的控制

The last parameter (true) specifies shared. Duration is specified by 60. Refer to the link How to: Cache Multiple Versions of a User Control Based on Parameters

回答你的第二个问题,让用户在运行时控制变量的缓存持续时间,你可以有两种方式:

To answer your second Q, to make the cache duration for the user control variable at run time, you can do it in two ways:

  1. 在后面的用户控制代码中赋值:

  1. Assign it in the user control code behind:

[PartialCaching(60, null, "UC_WebUserControl.BothParams", null, true)]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
    ...
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CachePolicy.Duration = new TimeSpan(0, 0, 60);
    }    
}

  • 您可以在使用用户控件的 ID 引用用户控件的页面后面的代码中分配它.

  • You can assign it in the code behind of the page where user control is referenced using the ID of the user control.

    例如如果aspx上的用户控件是:

    e.g. If the user control on the aspx is:

    <mycontrols:control1 ID="ucControl1" runat="server" param1="15" param2="20" />
    

    那么在aspx后面的代码中,你应该写:

    then in the code behind of aspx, you should write:

    this.ucControl1.CachePolicy.Duration = new TimeSpan(0, 0, 60);
    

    仅供参考,如果用户控件和页面都被缓存:如果页面输出缓存持续时间小于用户控件的缓存持续时间,则用户控件将被缓存直到其持续时间到期,即使页面的其余部分被缓存为请求重新生成.例如,如果页面输出缓存设置为 50 秒并且用户控件的输出缓存设置为 100 秒,则用户控件每两次过期页面其余部分过期一次.

    FYI, if both the user control and page are cached: If the page output cache duration is less than that of a user control, the user control will be cached until its duration has expired, even after the remainder of the page is regenerated for a request. For example, if page output caching is set to 50 seconds and the user control's output caching is set to 100 seconds, the user control expires once for every two times the rest of the page expires.

    这篇关于在 ASP.NET 中使用 PartialCaching 因控件属性而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆