Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data cache vs session object in ASP.Net

Should dynamic business objects for a site be stored in the users session or use ASP.Net caching (objects such as orders, profile information etc)?

I have worked with sites that used sessions to store business objects, but I was wondering...What are the advantages or disadvantages of caching?

like image 768
Miyagi Coder Avatar asked Jan 29 '09 15:01

Miyagi Coder


2 Answers

If the objects are shareable between user sessions, then use the cache. If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data.

like image 95
tvanfosson Avatar answered Sep 20 '22 23:09

tvanfosson


Caching is just that -- caching. You can never rely on entries being there, so no assumptions must be made in that respect: be prepared to go straight to the DB (or wherever else) to refetch data.

Session, on the other hand, is more suited towards storing objects, though personally I try to avoid session store in favour of a DB. I usually do that by abstracting away the store behind an opaque ISessionStoreService interface:

interface ISessionStore
{
    T GetEntry<T>(string key);
    void SaveEntry<T>(string key, T entry);
}

and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore, DbSessionStore or whatever.

like image 45
Anton Gogolev Avatar answered Sep 20 '22 23:09

Anton Gogolev