Cache VS Session VS cookies?

What are the do's and don'ts about Cache VS Session VS Cookies?

For example:
I'm using Session variables a lot and sometimes have problems in a booking-application when users start to order products and then go to lunch and come back some hours later and continue the booking. I store the booking in the session until the user confirms or aborts the booking so I don't need to talk to the database and handle halfway bookings in the database when users just click the X in the browser and never comes back.

Should I instead use cache or cookies or some combination for this?

(Also when there is some error in the app, the session-object resets itself and I get more problems because of that)

I'm mostly doing desktop-programming and feel I lack lots of knowledge here so anyone who can expand on where to use Cache, Session, Cookies (or db) would be appreciated

Edit: From the answers it seems that a combination of DB and cookies is what I want.

  • I have to store the booking in the database connected to a session-id
  • Store the session-id in a cookie (encrypted).
  • Every page load checking the cookie and fetch the booking from the database
  • I have a clean-up procedure that runs once a week that clears unfinished bookings.
  • I can't store the booking as a cookie because then the user can change prices and other sensitive data and I had to validate everything (can't trust the data).

    Have I got it right?

    And thanks for great explanations to all of you!


    State management is a critical thing to master when coming to Web world from a desktop application perspective.

  • Session is used to store per-user information for the current Web session on the server. It supports using a database server as the back-end store.
  • Cookie should be used to store per-user information for the current Web session or persistent information on the client, therefore client has control over the contents of a cookie.
  • Cache object is shared between users in a single application . Its primary purpose is to cache data from a data store and should not be used as a primary storage. It supports automatic invalidation features.
  • Application object is shared between users to store application-wide state and should be used accordingly.
  • If your application is used by a number of unauthenticated users, I suggest you store the data in a cookie. If it requires authentication, you can either store the data in the DB manually or use ASP.NET profile management features.


    Web is by nature disconnected model and none of the options mentioned (Session, Application, Cache, ...) are reliable enough. Session will timeout, worker process recycles, etc.

    If you really need to store the users progress, reliably and through extended periods, the database is your only solution. If you have users profile (if the user must log in), then it's straightforward. If not, generate a unique Id, store it in the cookie (or URL) and track the user based on that identification.

    Just make sure the Id is encrypted and then base64 encoded string and not just a numeric value.

    EDIT:

    After your additional explanation in the original question and comment from Mehrdad Afshari, good solution for you would be to use Session but set the storage to Sql Server instead of InProc.

    Here's more details and instructions how to set it up: http://msdn.microsoft.com/en-us/library/ms178586.aspx

    Have in mind that you will STILL have the session timeouts, but they will survive application pool recycles, even server restarts.

    If you truly need a permanent storage, custom solution with the database, as I originally outlined is the only solution.


    You should not use the Cache-object to cache session data, for the cache is shared between all users. Instead you could use Asp.Net Profile properties to store your data or you could add an event handler to the Session_End event and store the data if the user leaves the computer for too long.

    链接地址: http://www.djcxy.com/p/60564.html

    上一篇: 缓存用户控件并以编程方式清除该缓存

    下一篇: 缓存VS会话VS Cookie?