Flash3
Flash3

Reputation: 58

Choosing Cache versus Session to store data in a web forms application

I just want to know about ASP.NET cache and I'll just put my question on this simple example.

I use cache on a page "samplepage.aspx" (I am not using this method right now.)

// upon entering of the samplepage.aspx, assign user id to cache
Cache["loginid"] = Login.Id.ToString();

user1 enters on that page and currently using that cache object. Eventually, user2 enters also on the same page which is the "samplepage.aspx" while user1 is currently logon.

Now my question is, will the Login.Id of the user2 could overwrite the cache "loginid" which the user1 already assigned its Login.Id?

Or it will create another memory for cache object for user2? And if I put a List (let's say a list of 5000 employees) into cache, could it affect my system's performance? Also, which consumes more memory, cache or session if I put the same data in them?

I used session before but I want to know if cache could behave like session. Thanks for the answers.

EDIT:
@TimMedora, thanks for your comment but what if the list was filtered every event of a certain user on that page? Example if a user1 click button_filterLastNameOnly, and user1 will click another button_filterFirstNameOnly. As you see, that list will be filtered based on the event of the user1. I don't know where to save that list because I read this article about reducing round trips from the database so I want to save that list on a cache or a session.

Upvotes: 1

Views: 2315

Answers (2)

Justin Lessard
Justin Lessard

Reputation: 11901

See Data cache vs session object in ASP.Net.

Basically, for user specific objects (like in your case, the user id), use the session. For object shared by all user, use cache.

Upvotes: 2

Misha Zaslavsky
Misha Zaslavsky

Reputation: 9646

Cache performance is good so don't worry about using it.

In your example, it is much better to save your user information in the Session because it is a unique information for each user. If you wanted to save something common for all users, then you could go for Cache.

Upvotes: 1

Related Questions