I have implemented the Redis OutputCache (Microsoft.Web.RedisOutputCacheProvider) using the Azure Redis service.
I have found similar unanswered / unsolved questions here and here.
On top of the pages need to cache:
<%# OutputCache VaryByParam="*" Duration="600" %>
After access a random page and I check the key in Redis database is has saved like: /_a2/monitor.aspx
So running the below line of code, will remove this cache item (and it worked).
HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")
Now I have updated the same page adding the varyByCustom the the OutputCache directive:
<%# OutputCache VaryByParam="*" varyByCustom="userhash" Duration="600" %>
Now accessing the page and checking the key in Redis database it is saved like: /_a2/monitor.aspxHQFCNuserhashVcc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59DE
You see the added userhash hash value, this is nice and worked as expected.
But now here comes the problem: How do I clear this cache item.
This is not working:
`HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")`
Also tried:
HttpResponse.RemoveOutputCacheItem("/monitor.aspx?userhash=cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59", "RedisOutputCache")
And also tried:
HttpResponse.RemoveOutputCacheItem("/monitor.aspx {userhash:cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59}", "RedisOutputCache")
Also tried using some of the below options in combination of the above code:
Response.Cache.SetVaryByCustom("userhash")
Response.AddCacheItemDependency("action")
HttpContext.Current.Cache.Item("action") = "test"
Response.Cache.VaryByParams("userhash") = True
HttpResponse.RemoveOutputCacheItem("/monitor.aspx")
Is there an way to remove this cache item on using the varyByCustom option?
I don't use the system.web.mvc object. So I can not access this object for URL helper.
Any help is most welcome!
Joël
According to your description, I tried to implement my OutputCacheProvider to store cache into files to test this issue on my side. Here is my test result, you could refer to it.
OutputCache of my Monitor.aspx
<%# OutputCache VaryByParam="None" Duration="60" Location="Server" %>
When accessing the above page, I would get the following log from my OutputFileCacheProvider as follows:
And the current DateTime string would be refreshed in my monitor.aspx page after I have accessed another .aspx to clear the cache.
<%# OutputCache VaryByParam="*" Duration="60" VaryByCustom="userhash" Location="Server" %>
Note: The same way to call HttpResponse.RemoveOutputCacheItem("/monitor.aspx") for cleaning cache from another .aspx endpoint.
Both VaryByParam and varyByCustom could work as expected on my side. I assumed that you could add log within your OutputCacheProvider and try to find whether you have missed something.
Related
I have a page in which i have 4 user control. 1 of them is used to filter out the the contents of the other control. I have checkboxes in that control like filter by gender, filter by size and color etc....
I have added the following code on my page for caching. it seems to work 2-3 times when you filter it like select genders checkbox and it works, if you select size filter it works, but again if you select gender filter it does not work and other control shows no results message. when i know there are results because first time it did work.
I am adding a query string parameter on each filters so that i can get the results back.
<%# OutputCache Duration="300" VaryByParam="*" %>
Query string
http://somwsite.com/jacktes/?n=0&lp=&hp=&size=&colour=&g=MENS
I have tried the following with no luck.
<%# OutputCache Duration="300" VaryByParam="none" VaryByControl="GenderFilter" %>
<%# OutputCache Duration="300" VaryByControl="GenderFilter" %> // ID of the checkbox List
<%# OutputCache Duration="300" VaryByParam="n;lp;hp;size;colour" %> // ID of the querystring param
can anyone please help me with this?
Note: No error message as page works fine BUT out of 4 control the control which shows the filter results is displaying no results.
stumbled on this while trying to find a better solution
so basically there is a workaround:
OutputCache returns invalid version with PostBack
I have a UserControl, which should only change based on 2 URL parameters.
The problem is, it has a public property, which is used in the calling pages, so it throws a NullReferenceException on my property.
Any ideas?
I think I've figured this one out, it seems to be quite tricky which is due to my lack of comprehensive understanding of how output cache works I suspect.
You can't cache the UserControl if it has variable properties that dicatate it's content. You need to put a cache control in the Content page that holds the control. Then add the cache to the content page:
<%# OutputCache Duration="120" VaryByControl="JobList" %>
Where the vary by control is the ID of the control you wish to cache. Then specify a property for that vary by control:
<%# OutputCache Duration="120" VaryByControl="JobList.LoggedInUserID" %>
This seems to work for me!
Check the VaryBy options,
try to take a look to this articles:
http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.71%29.aspx
https://web.archive.org/web/20211020113508/https://www.4guysfromrolla.com/articles/022802-1.aspx
http://weblogs.asp.net/stefansedich/archive/2008/03/17/output-cache-with-usercontrol-on-masterpage-and-multiple-varybycustom.aspx
I have a user control registered at the top of my page:
<%# Register Src="/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
and I reference it in my page like this:
<pmc:Navbar runat="server" id="navbar"></pmc:Navbar>
but it does not know what <pmc:Navbar is. I cannot figure out why.
I'm using VS 2008, in a Web Application Project.
Maybe you should specify the path with ~: ... Src="~/Controls/User/Navbar.ascx" ...
Remove either the initial slash from the path to the control, or better still, prefix it with "~" :
<%# Register Src="Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
or
<%# Register Src="~/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
The first solution is flakey as it relies on the page existing in the root folder and the control existing below it. The second is the preferred as it will work from any page in your project.
You should also consider registering your user controls in your web.config, as it keeps things much neater, and tends to avoid path issues a little better.
If I use Substitution control in asp.net page, and also add the following directive to the page:
<%# OutputCache Duration="7200" VaryByParam="None" Location="Any" %>
Would the location attribute be ignored, since using Substitution control on a page makes the page cacheable only on the server?
Yes, it will be ignored. Substition.Render calls, through RenderMarkup, HttpResponse.WriteSubstitution that calls HttpCachePolicy.SetCacheability(HttpCacheability.Server).
I am not sure why I cannot get simple output cache to work and not sure of how to debug a situation like this.
Simply from looking at examples and previous projects I have worked on, I used the following code:
<%# OutputCache Duration="100" VaryByParam="id" %>
but since that didn't work I tried VaryByParam="*" and VaryByParam="none"
None of these worked.
Are there quick checks to make sure something else is not interfering with OutputCache?
If you're just trying to see if OutputCache is working correctly, you can always add a label to your page that just binds to the current datetime. The first time you load it, it will give you the timestamp of when the label was bound.
The next time the label should update is after the cache has expired and the page is hit again.
<%# OutputCache duration="120" varybyparam="None" varybycustom="Browser" %>
<asp:Label ID="Testlabel" runat="server" Text=""><%= DateTime.Now %></asp:Label>
You are only caching it for 100 seconds, have you tried a longer value? Starting the debugger in Visual Studio could take 100 seconds (or longer!). Also have you tried without the VaryByParam?
This is similar to womps answer with a few corrections.
If you're just trying to see if OutputCache is working correctly, you can always include the current datetime. The first time you load it, it will give you the timestamp of when the label was bound. In other words the cache is primed. If you reload the page the date should be the same as the output is cached. Once the cache is expired the date time will update again and the cycle repeats.
<%# OutputCache duration="120" varybyparam="None" %>
<%= DateTime.Now %>