[RESOLVED] Cache dynamic menu

I'm dynamically creating an asp:menu and asp:treeview (2 separate navigation controls) in my master page based on database entries. What's the best way to prevent these from being re-created every time the user selects a new page (seems like a lot of redundant database calls). Can I somehow cache these objects? Or should I be storing the database values in a session variable which I can re-build from on each new page? Just hoping someone can point me in the right direction.

I recently implemented something similar using cached ArrayLists.  I needed to populate some dropdowns based on DB values and didn't want to make the call over and over again for performance issues:

Custom Class to build out the list: 
returnSizes = new ArrayList();
...some code here to populate the list...
string sizescached = "sizescached" + category + attribute; //Built out a custom name for the cached item based on a category and attribute name
HttpRuntime.Cache.Add(sizescached, returnSizes, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero, CacheItemPriority.Default, null); //added it to the cache for 30 minutes at a time

In the page that uses the dropdown:
string sizescached = "sizescached" + curCategory + curAttribute;  //Build out a custom name based on current category and attribute name
ArrayList testCachedSizeList = (ArrayList)Cache[sizescached]; //Pull in the cached arraylist
if (testCachedSizeList == null) //Test the list to see if it has data, if not go pull the list from the custom class

This works like a charm.  This first user to hit the page after the 30 minute time-out experiences a slight delay in page load, but after that its golden.  We used the same technique on our menus as well, and cached them for 8 hour time frames.

Excellent. This looks like just what I was looking for. Thank you!

hi tehremo, thanks your tips. i am just curious. if am not mistaken, even using cache, you still need to create the menu (one by one) on each page right? so, it still need sometime to create the menu especially if the menu has large data.

can we just eliminate the proses to recreating the menu on each page ?

please share..... thanks.......

1 thoughts on "[RESOLVED] Cache dynamic menu"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>