Caching vs Session

V

Vivek Sharma

Hi,

I have a dynamic menu that is built from the database. Since the menu is
used on various pages I wish to reload the menu on the pages. I have 2
options in my mind. One I use sessions for the dataset and use the session
to load the menu. The other option is to cache the dataset or the menu user
control. Which is a better option? Is there any other option?

Thanks
Vivek
 
V

Vivek Sharma

Hi,

Also, wants to mention that the menu is generated from the database
depending on which user is logged on.

Thanks

Vivek
 
A

Andrew Robinson

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.
 
D

Daniel Walzenbach

Why not use caching? You could store in information in cache with the UserID
to make it unique. I don't think that it's a good solution to get the
information from the database on every request.

Regards

Daniel
 
K

Karl Seguin

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
 
D

Daniel Walzenbach

Vivek,

go with Karl's suggestion and have a little background reading:
http://www.msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt06.asp

CU

Daniel Walzenbach


Karl Seguin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top