0

I can't understand which container should I use for storing cache:

HttpContext.Current.Session or MemoryCache?

What is difference between data stored in HttpContext.Current.Session and MemoryCache?

matt2244
  • 23
  • 3
  • Memory, database, Redis. Depends on what you want to cache and whether you have one server or many. Memory only works if you have a single server. `Session` isn't a cache, it stored data for the current session only and may use memory or a database for storage. Is an actual in-memory cache that is accessible by all requests. – Panagiotis Kanavos May 10 '17 at 09:21
  • `MemoryCache` is not a ASP.Net specific caching mechanism. It can well be used by a windows forms application as well. When you are thinking of storing some data in your ASP.Net website which should persist across web calls then you should think in terms of [Application vs Session vs Cache](https://stackoverflow.com/q/5096544/465053). Cache in ASP .Net application refers to `System.Web.Caching.Cache` class (in System.Web dll) which is not same as `System.Runtime.Caching.MemoryCache` (in System.Runtime.Caching.dll) class. – RBT Jan 19 '18 at 03:11

2 Answers2

2

you could user HttpContext.Current.Session when you are storing data for a specific user

MemoryCache is when you are storing data for all users, the data are shared between all users

Tarek Abo ELkheir
  • 1,311
  • 10
  • 13
  • 1
    `Session` stores data for specific *sessions* not users. If the user logs out, the data is gone. – Panagiotis Kanavos May 10 '17 at 09:19
  • Furthermore, a session may use memory or a database for storage. Database storage is the only option if you have 2+ servers. Session is not a good choice for caching. – Panagiotis Kanavos May 10 '17 at 09:22
  • @PanagiotisKanavos, *Database storage is the only option if you have 2+ servers...* No, you should consider using a `Pooled Session` instead. – Rahul May 10 '17 at 09:31
  • @Rahul which stores the data where? If you have many servers, you need shared storage. – Panagiotis Kanavos May 10 '17 at 09:32
  • @PanagiotisKanavos, I mean not only DB .. it could be even *State Server Mode* in which case session object is stored in separate process – Rahul May 10 '17 at 09:36
  • Which is another type of database. On that *isn't* used much in fact, especially in hosted, cloud or high-traffic environments – Panagiotis Kanavos May 10 '17 at 09:37
  • @PanagiotisKanavos I didn't get your point - `Session is not a good choice for caching.`. Cache has a very broader meaning which includes all three namely Application State, Session State and Web Cache. The idea of caching in general is that you just don't want to fetch something time and again and keep it handy in memory. Scope access and recommended sizing is what differentiates the various means of caching. So if scope access is user-specific then of course you would use Session state as a mean of caching the information. Kindly correct me if I'm wrong. – RBT Jan 19 '18 at 23:41
1

Your sole question is: difference between Session and Cache ... well Session is specific to account or user account (each user specific access is part of session) whereas Cache is global to the application. You store only common data which would be used across the application irrespective of user session in cache. Now again your cache store could be persistent (if you chose to store in DB or in distributed cache like Redis or Azure memcache / non-persistent like ASP.NET HttpContext.Cache object which stores the cache object in worker process w3wp ... thus in case worker process dies by means of recycling the application pool, you loose the cache object)

Moreover by means of session, if you want to access the session across all the web servers (in a load balanced environment) then you should consider using a Pooled Session

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • What do you mean "pooled session"? Googling for `ASP.NET pooled session` doesn't return any results. Do you mean something else? BTW web farms *do* use shared storage, whether is a SQL or Redis database. To the web site, these are all external databases – Panagiotis Kanavos May 10 '17 at 09:34