Programmatic use of SetVaryByCustom

  • Thread starter Thread starter DV
  • Start date Start date
D

DV

Has anyone experienced any problems with the use of
Response.Cache.SetVaryByCustom within their code?

I have page which is set to cache the contents for a
certain time (time varies depending on url)

Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires((DateTime.Now.AddSeconds
(obj.CacheTimeout)));
Response.Cache.VaryByParams["*"] = true;

And I cache per user via
Response.Cache.SetVaryByCustom("authuser");

This works fine.

BUT if I programmatically decide whether to use the custom
auth or not the cache is cleared for all cached versions
of the page (regardless of the url arguments).
e.g.

if(obj.Confidential)
Response.Cache.SetVaryByCustom("authuser");

e.g. The page I am caching generates a chart and these
charts can be included several times within a page. If
some charts use SetVaryByCustom and some don't then no
caching occurs. If all charts use SetVaryByCustom then all
charts are cached per user.

many thanks,

Darren
 
I don't know if this will help have your tried adding
Response.Cache.SetValidUntilExpires(True)

DV said:
Forget to mention that, yes, I have implemented
GetVaryByCustomString within global.aspx
-----Original Message-----
Has anyone experienced any problems with the use of
Response.Cache.SetVaryByCustom within their code?

I have page which is set to cache the contents for a
certain time (time varies depending on url)

Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires((DateTime.Now.AddSeconds
(obj.CacheTimeout)));
Response.Cache.VaryByParams["*"] = true;

And I cache per user via
Response.Cache.SetVaryByCustom("authuser");

This works fine.

BUT if I programmatically decide whether to use the custom
auth or not the cache is cleared for all cached versions
of the page (regardless of the url arguments).
e.g.

if(obj.Confidential)
Response.Cache.SetVaryByCustom("authuser");

e.g. The page I am caching generates a chart and these
charts can be included several times within a page. If
some charts use SetVaryByCustom and some don't then no
caching occurs. If all charts use SetVaryByCustom then all
charts are cached per user.

many thanks,

Darren









.
 
Thanks for the tip - sadly it didn't fix the problem.

It's weird how the framework can cache the page based on
the url and have different expiry times for each one but
not support have varybycustom set for some but not all.

D
-----Original Message-----
I don't know if this will help have your tried adding
Response.Cache.SetValidUntilExpires(True)

DV said:
Forget to mention that, yes, I have implemented
GetVaryByCustomString within global.aspx
-----Original Message-----
Has anyone experienced any problems with the use of
Response.Cache.SetVaryByCustom within their code?

I have page which is set to cache the contents for a
certain time (time varies depending on url)

Response.Cache.SetCacheability (HttpCacheability.Server);
Response.Cache.SetExpires((DateTime.Now.AddSeconds
(obj.CacheTimeout)));
Response.Cache.VaryByParams["*"] = true;

And I cache per user via
Response.Cache.SetVaryByCustom("authuser");

This works fine.

BUT if I programmatically decide whether to use the custom
auth or not the cache is cleared for all cached versions
of the page (regardless of the url arguments).
e.g.

if(obj.Confidential)
Response.Cache.SetVaryByCustom("authuser");

e.g. The page I am caching generates a chart and these
charts can be included several times within a page. If
some charts use SetVaryByCustom and some don't then no
caching occurs. If all charts use SetVaryByCustom then all
charts are cached per user.

many thanks,

Darren
 
Hi DV,

Thank you for posting to the MSDN newsgroups.

I am interested in this issue and am researching on it now. I will update
you as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi DV,

Based on my research, it seems to be an issue by design. Only when all
parts on a web page applied the SetVaryByCustom, does the cache works.
However, I need to do a further confirmation, and I will post back here.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for the time taken already - it would seem you
concur.

The server cache supports different expiry times for the
different url arguments (same aspx) but it is a pity it
doesn't apply the SetVaryByCustom in a similar manner. It
seems the SetVaryByCustom can only be applied to the aspx
(regardless of url). If this is intentional then it may be
worth commenting on this within the documentation?

I look forward to any updates you may have...

Darren
 
Hello,

After studied the problem, I think the problem may not be related to the
expire time. As Jacob mention, we can only have one cache policy for a
single cached component. Since all of your charts are in same web form, I
think they should use same cache policy. If my understanding is inccorect,
please feel free to correct me.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
The aspx page chart.aspx returns the requested chart by
dynamically drawing the chart and includes the date
generated and the authenticated username that requested it
within the graphic.

The code of chart.aspx includes the following lines of
code:

Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires((DateTime.Now.AddSeconds(
chartid * 30 )));
Response.Cache.VaryByParams["*"] = true;
Response.Cache.SetValidUntilExpires(true);

#TEST A#
By loading display.htm as UserA in the browser and
repeatedly hitting refresh you can see that the generated
charts are cached and each chart gets refreshed using it
own assigned expiry time - as you'd expect. If you go to
another users machine (UserB) and do the same you'll
initially see cached charts which were generated for UserA
(and not UserB) - again as you'd expect.

Now add the line:
Response.Cache.SetVaryByCustom("authuser");

and implement the function below within global.aspx

public override string GetVaryByCustomString(HttpContext
context, string arg)
{
if (arg.ToLower() == "authuser" &&
context.User.Identity.IsAuthenticated)
{
return context.User.Identity.Name;
}
}

#TEST B#
Now by loading display.htm and repeating TEST A you will
find that each user has their own individual cached copy
of the chart. Again each chart will have it's own expiry
time. No surprises there.

But if we were to programmatically decide whether the
chart should be cached per user by adding

if(chartid % 2 == 0)
Response.Cache.SetVaryByCustom("authuser");

and then repeating 'TEST A' you'd expect some charts to be
shared between users and some not - what actually happens
is the cache (for all copies of chart.aspx -regardless of
url) are cleared when SetVaryByCustom is switch on and off
between generated charts effectively turning off caching.

I hope this explains the problem. It's not a show stopper
for me but I think it is an issue that should either be
documented or fixed.

Many thanks for taking an interest

Darren Voisey

[my hotmail address is obvious]
 
...the start of previous post my seems to have been cut
out...

display.htm contains HTML that contains 4 IMG tags with
their SRC attribute set to load from chart.apsx.

e.g

Image 1 would load from = chart.aspx?chartid=1
Image 2 would load from = chart.aspx?chartid=2
Image 3 would load from = chart.aspx?chartid=3
Image 4 would load from = chart.aspx?chartid=4


-----Original Message-----

The aspx page chart.aspx returns the requested chart by
dynamically drawing the chart and includes the date
generated and the authenticated username that requested it
within the graphic.

The code of chart.aspx includes the following lines of
code:

Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires((DateTime.Now.AddSeconds(
chartid * 30 )));
Response.Cache.VaryByParams["*"] = true;
Response.Cache.SetValidUntilExpires(true);

#TEST A#
By loading display.htm as UserA in the browser and
repeatedly hitting refresh you can see that the generated
charts are cached and each chart gets refreshed using it
own assigned expiry time - as you'd expect. If you go to
another users machine (UserB) and do the same you'll
initially see cached charts which were generated for UserA
(and not UserB) - again as you'd expect.

Now add the line:
Response.Cache.SetVaryByCustom("authuser");

and implement the function below within global.aspx

public override string GetVaryByCustomString(HttpContext
context, string arg)
{
if (arg.ToLower() == "authuser" &&
context.User.Identity.IsAuthenticated)
{
return context.User.Identity.Name;
}
}

#TEST B#
Now by loading display.htm and repeating TEST A you will
find that each user has their own individual cached copy
of the chart. Again each chart will have it's own expiry
time. No surprises there.

But if we were to programmatically decide whether the
chart should be cached per user by adding

if(chartid % 2 == 0)
Response.Cache.SetVaryByCustom("authuser");

and then repeating 'TEST A' you'd expect some charts to be
shared between users and some not - what actually happens
is the cache (for all copies of chart.aspx -regardless of
url) are cleared when SetVaryByCustom is switch on and off
between generated charts effectively turning off caching.

I hope this explains the problem. It's not a show stopper
for me but I think it is an issue that should either be
documented or fixed.

Many thanks for taking an interest

Darren Voisey

[my hotmail address is obvious]
-----Original Message-----
Hello,

After studied the problem, I think the problem may not
be
related to the
expire time. As Jacob mention, we can only have one
cache
policy for a
single cached component. Since all of your charts are in same web form, I
think they should use same cache policy. If my understanding is inccorect,
please feel free to correct me.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

.
.
 
hi Darren,

I think the key point is following:

"you'd expect some charts to be shared between users and some not"

With SetVaryByCustom and a same aspx form, the ASP.NET cache will only
apply the custom policy to a same aspx, it can't allow a copy without a
"user id" as index.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top