7

After creating a proof of concept for an ASP.NET MVC site and making sure the appropriate separation of concerns were in place, I noticed that I was making a lot of expensive redundant database calls for information about the current user.

Being historically a desktop and services person, my first thought was to cache the db results in some statics. It didn't take much searching to see that doing this would persist the current user's data across the whole AppDomain for all users.

Next I thought of using HttpContext.Current. However, if you put stuff here when a user is logged out, then when they log in your cached data will be out of date. I could update this every time login/logout occurs but I can't tell if this feels right. In the absence of other ideas, this is where I'm leaning.

What is a lightweight way to accurately cache user details and avoid having to make tons of database calls?

Dinah
  • 52,922
  • 30
  • 133
  • 149
  • What sort of data do you need to store? If it's just a few simple properties ie Strings, Integers then you could just store in the forms cookie. – willbt Jan 15 '10 at 22:46

3 Answers3

5

If the information you want to cache is per-user and only while they are active, then Session is the right place.
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx

AUSteve
  • 3,238
  • 21
  • 36
  • Here's another great post working on top of Session: http://stackoverflow.com/questions/343899/ – Dinah Jan 19 '10 at 19:22
  • Session is really per browser session. You can log in as a different user using the same browser, and the Session State returned will be the same as for the previous user. – Appetere Feb 20 '13 at 14:43
  • @Steve if that's the case then the application is not handling log on/off correctly. – AUSteve Mar 05 '13 at 00:46
  • I know this is old but wanted to make sure people thought about this as well. Session state, by default, is limited to the web server the application is on. If you are load balancing and want to use session state, you need to use a SQL Server database to store your sessions, an ASP.NET state server, or a custom server. See the **Remarks** section [here](https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx) – fujiiface Nov 21 '16 at 16:07
3

What you're looking for is System.Web.Caching.Cache

http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

willbt
  • 1,875
  • 13
  • 11
  • 5
    To quote MSDN "One instance of this class is created per application domain" which is not what the OP wanted. – AUSteve Jan 15 '10 at 20:56
  • 1
    I am using HttpContext.Current.Cache to store IPrincipal with FormsAuthentication. Session means another cookie and additional problems. – LukLed Jan 15 '10 at 21:25
  • 1
    I'm well aware that one instance is created per AppDomain but whats wrong with adding and retrieving from the cache with a unique key for the user. e.g. User-1 where 1 is the Id of the user? It still solves the problem IMO. – willbt Jan 15 '10 at 22:44
  • 1
    You could also use HttpContext.Current.Items which is per user, but also per request which might not be what you want. – The Muffin Man Oct 23 '15 at 14:15
3

ASP.NET session-state management is good for some situations but when heavy load is put, it tends to create bottlenecks in ASP.NET performance. Read more about it here:

http://msdn.microsoft.com/en-us/magazine/dd942840.aspx

http://esj.com/articles/2009/03/17/optimize-scalability-asp-net.aspx

The solution to avoid bottlenecks is use of distributed caching. There are many free distributed caching solutions in the market like Memcached or NCache Express.

Dont know much about Memcached but i've used NCache Express by Alachisoft, it lets you use ASP.NET caching without requiring any code change.

graham
  • 141
  • 2