Partial Caching AddCacheItemDependency problem

Discussion in 'ASP .Net' started by the4man, Feb 22, 2006.

  1. the4man

    the4man Guest

    Hi all!!

    I have a very stupid and simple project, just for testing, with only
    one page (default.aspx), and one user control. The user control only
    shows the time in an asp:literal each time the page is requested.

    I want to cache the content of the user control (not the entire page),
    and I want to invalidate that cache when I need. To do this, I have
    added an "OuputCache" directive in my USER control. Furthermore I have
    a button to invalidate the cache. This is the user control code:

    protected void Page_Load(object sender, EventArgs e)
    {
    Literal1.Text = DateTime.Now.ToLongTimeString();
    Response.AddCacheItemDependency("cachecontrol");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    HttpContext.Current.Cache.Insert("cachecontrol", DateTime.Now);
    Response.Redirect("~/Default.aspx");
    }

    In the global.asax, I have this:

    void Application_Start(object sender, EventArgs e)
    {
    HttpContext.Current.Cache.Insert("cachecontrol", DateTime.Now);
    }

    This code is base in the examples of this article:
    http://aspalliance.com/668

    Well, it does not work. All my attemps to invalidate the cache of the
    user control have been unsucessfull. Only if I put the "OutputCache"
    directive in the default.aspx (what means cache the entire page) the
    cache invalidation mechanism works.

    Can anyone help me? How can I invalidate a partial-cached web page?

    Thanks in advance!
     
    the4man, Feb 22, 2006
    #1
    1. Advertising

  2. the4man

    the4man Guest

    Answer my self:

    >From microsoft.com

    (http://msdn2.microsoft.com/en-us/library/t1d120ks.aspx ):

    You cannot call the AddCacheItemDependency method in an ASP.NET user
    control. However, in any user control that specifies the @ OutputCache
    directive, you can create a CacheDependency object that describes the
    cache key dependency and assign it to the Dependency property of the
    UserControl object.

    solution:

    User control code:

    protected void Page_Load(object sender, EventArgs e)
    {
    Literal1.Text = DateTime.Now.ToLongTimeString();
    String[] dependencyKey = new String[1];
    dependencyKey[0] = "cachecontrol";

    BasePartialCachingControl pcc = Parent as
    BasePartialCachingControl;
    pcc.Dependency = new CacheDependency(null,
    dependencyKey);
    }

    Default.aspx code (note that now the button to invalidate the cache is
    outside the user control):

    HttpRuntime.Cache.Insert("cachecontrol", DateTime.Now)

    Global.asax:

    void Application_Start(object sender, EventArgs e)
    {
    HttpRuntime.Cache.Insert("cachecontrol", DateTime.Now);
    }
     
    the4man, Feb 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Gregg

    User Control Partial Caching

    Ryan Gregg, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    385
    Ryan Gregg
    Jul 18, 2003
  2. Billy
    Replies:
    2
    Views:
    524
    Billy
    Feb 1, 2006
  3. Rolf Welskes
    Replies:
    6
    Views:
    616
    Rolf Welskes
    Oct 24, 2006
  4. Thomas Heller
    Replies:
    13
    Views:
    887
    Michele Simionato
    Feb 8, 2007
  5. J. Clifford Dyer

    Re: Partial 1.0 - Partial classes for Python

    J. Clifford Dyer, Feb 8, 2007, in forum: Python
    Replies:
    0
    Views:
    536
    J. Clifford Dyer
    Feb 8, 2007
Loading...

Share This Page