1

I am using master page in which menu is generating dynamically according to user role in code. The same menu is used for all application to a particular user up to log out. So instead of recreating it, i need the same menu for all of the application. The Menu is in StringBuilder which is very large size. Is Session or Data cache is better and less memory consuming in my situation and why. Please suggest?

I want to improve performance of master page.

Thanks

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Areeb
  • 219
  • 1
  • 4
  • 14

2 Answers2

1

I think Cache will be better as you will have only one instance created for one role, but Session will make it create multiple instance as many as user is accessing, and you will have to wait session timeout sometimes to free the memory

Simon Wang
  • 2,843
  • 1
  • 16
  • 32
  • supose i have 4 roles. that means only 4 menu is cached in memory and according to user role user used these menu m i right ? – Areeb Mar 01 '12 at 13:32
  • yes, or maybe you can just put it into Application State which sound more reasonable as you do not need to refresh them, cache is suppose to store data that need to refresh and your web server may drop cache objects in heavy load, but Application State can be kept forever, check this: http://msdn.microsoft.com/en-us/library/94xkskdf.aspx – Simon Wang Mar 01 '12 at 13:47
0

If every user will get the same menu:

You should consider putting it in the Application "Cache" - Application["MyMenu"] or a static field on one of your objects.

The main reason for this is lifetime. If you put it in an application level object, then it will last for the lifetime of the application. Putting it in a session level object will cause it to be lost when that session ends - as a session is started per user, then you will soon find yourself recaching the data.

On the other hand... if it's unique per user:

The session provides a handy place to put this data, as it is unique to that user, and will not live long beyond that user leaving the site.

Also think about:

If you really think memory is going to be an issue or you want to define exactly how long you keep it for

Put it in the Cache. You can determine the amount of time it lives in the cache, and, additionally, the cache will start to dump objects when it gets short on memory - so it is more sensitive to load than the other options.

There is a good discussion of Session vs Cache on SO already

Additionally

Are you sure your menu is that big? If it is, you might want to consider alternatives - just how big are you talking?

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71
  • Yes memory is issue and string is around 113 lines and for every user menu is different – Areeb Mar 01 '12 at 13:30
  • Then it belongs in the cache, and your code will 1) Build Menu, 2) Put it in the cache with a key for that user and 3) return the menu. The retrieval code will 1) Check if the menu is in the cache 2) If it is, return that or 3) Build Menu and put it back in the cache before returning it. You need to balance cost of retrieval with memory used for caching. – dash Mar 01 '12 at 13:57