asp.net-mvcdata-storagesession-less

MVC 3 - Sessionless controllers datastore options


i've been reading about sessionless controllers lately and it seems an interesting idea, since it improves perfomance and let ajax calls to be asynchronous, as usually they should be.

However, i can't figure a nice way to store data that would previously stored by a session. I have a lot of single-fetch data that i get once and walks with it through several pages. My first thought was to use MemoryCache, but reading this post i begin to doubt it, since IIS can let go my data anytime.

Because of this, i got a little confused on what should i do to store data in a session like way. I read a couple of thing about NoSQL and MongoDB, but wouldn't that be the same as to fetch data all the time i need it?

Can you give me some clarifications and technologies i can use to serve as temporary datastore?


Solution

  • Have you considered using the HttpContext.Cache? As you're saying in a session like way, there is no reason you couldn't create a cache key based upon the sessionid of the current request:

    // cache key
    var cacheKey = string.Format("{0}-{1}", "SomeKey", Session.SessionID);
    
    // save to cache
    HttpContext.Cache.Insert(cacheKey, <yourobject>, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
    

    From there it would simply be a matter of passing along the sessionid and retrieving at a later time:

    HttpContext.Cache[cacheKey]