Yes and no. Your assessment is 100% right, but in my experience, not
complete.
Session and cache are different in more than their scope. It's true that
scope of sessions is much better suited for this task.
However, sessions pin things in memory. You are guaranteed the data will
be there unless the app restarts. This can put a strain on memory since
you've limited ASP.Net's ability to manage this memory.
Cache on the other side uses weak references, and thus lets ASP.Net manage
the memory used by it much better. In almost all circumstances, i much
prefer this behaviour.
It means that to access a value from the session, you do
DataSet ds = (Dataset)Session["Menu"];
//ds is ready to be used!
but for the cache, you need to be more defensive:
DataSet ds = (DataSet)Cache["Menu"];
if (ds == null)
{
ds = GetDataFromDataBase();
Cache.Add("Menu", ds);
}
return ds;
Unless you have good reason to pin things in memory (and there certainly
are such reasons out there), you should try to avoid doing it.
For your situation, since it varies from user to user, you'll need to
modify your cacheKey per user, such as Cache["Menu" +
currentUser.UserId.ToString()]; or something.
Not the most elegant
I wish they'd come out with a mix of the two, a session-based weak
reference storage, SessionCache["Menu"]...oh well, maybe 3.0
The choice is yours, I'm certainly cached biases, but I hope I've given
you a fuller picture.
THE SHORT ANSWER:
If you didn't understand a word I said, use the Session
if you understood what I said, then you'll have to decide for yourself
(even I'd use the session object in very simple situation...well, no I
wouldn't, but oh well...)
karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
Andrew Robinson said:
Using the server based cache would apply to all users so that is not an
option. Session sounds like the way to go. Also, you could just reload it
from your database on each page.
--
Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org