在ASP.NET中使用PartialCaching控件的属性各不相同 [英] Vary by control properties using PartialCaching in ASP.NET

查看:176
本文介绍了在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" />

但是,如果在两个单独的页面两个控制实例有相同参数1和参数性能,我想他们缓存作为一个对象(这样的缓存控制,将共享)。

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,让我先告诉你,你的问题本身所具有的答案)。 共享......是的这就是关键字:)要在缓存中的单个实例在所有页面的用户控件,在@OutputCache指令设置共享=真。这应该在该网页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指定的持续时间,请参阅链接如何:缓存基于参数用户控件的多个版本

要回答你的第二个Q,使在运行时用户控制变量的缓存持续时间,你可以做到这一点有两种方式:

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. 指定它后面的用户控件code:

  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所引用的页面背后的code分配给它。

  • 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背后的code,你应该写:

    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天全站免登陆