1

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?

Community
  • 1
  • 1
AdrianoRR
  • 1,131
  • 1
  • 17
  • 36
  • I never use sessions (just an authorization cookie). What kind of data do you need to store? – jgauffin Jun 04 '12 at 14:15
  • @jgauffin I have a financial application and there's a central page that fetchs a lot of data. Having to fetch this data everytime the user moves to other pages is simply not interesting. Usually they are lists of ViewModels which i get them from the database. If i don't use session i'd have to search everytime for the same kind of data when the user goes to others pages. – AdrianoRR Jun 04 '12 at 14:47

1 Answers1

0

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]
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • Wouldn't this simply be moving from one type of session persistence to another? – Jared Peless Jun 04 '12 at 15:28
  • Isn't that what your were asking? – Jesse Jun 04 '12 at 15:43
  • I wasn't asking the original question. I guess to clarify I would add... "how does that change the performance issues and farm scenarios, etc. present with sessions?" In other words, in which way is the HttpContext caching superior to sessions, other than the flexibility of expiration (which you are showing no absolute anyway)? – Jared Peless Jun 05 '12 at 17:59
  • 1
    @JaredPeless I mentioned using httpcontext in place of session as I recenly switched away from session in one of the applications I'm working on and noticed a significant improvement in view rendering per request. further reading: http://dotnetslackers.com/articles/aspnet/SessionCache.aspx – Jesse Jun 05 '12 at 22:01
  • @Jesse i was thinking about using a NoSql DB in AppData to store my data, but seeing this article that you've linked, made me choice for `HttpContext.Current.Cache`. I don't even know anymore why ASP.NET MVC still uses Session. – AdrianoRR Jun 14 '12 at 00:15