11

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?

5 Answers 5

24

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Another consideration would be the level of control and granularity needed for the lifetime of the cached data. Session limits you to a sliding expiration strategy based on the global session state timeout period. Using the cache directly gives you additional control over these parameters.
7

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.

1 Comment

UpVoted! Excellent suggestion! Do you have an in memory implementation example for ISessionStore in c#?
2

The ASP.NET system cache is global to the application where as the session is unique to the current user. If you chose to use the global cache to store objects you would need to create an object identification strategy so you could get the correct objects for your per user basis.

If you are looking to enhance performance you would be better off replacing the ASP.NET session state with a distributed memory cache such as Microsoft's velocity. Microsoft has posted articles on how to replace session usage to target Velocity. You could also use Memcache or other similar products in a related fashion.

Comments

2

Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application.
The key to save in one or the other is to determine if what are you trying to store will be user-only data or it needs to be shared in all the application.

Session => A webpage with a step by step interface (an online test for example).
Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page).
Hope this helps.

Comments

1

Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. Imagine you that you have a process of getting 1000 record from database (and it take about 3 seconds) and you will need it for a few minutes. You can store your objects in Cache and set expire date, priority and dependency to it (like SqlDependency or FileDependency), so for next requests you can use Cached data instead of retriving it from database. You can store your object in Session but you can not set dependency for Session by default. Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.