User session state is relevant to web world while the newer MemoryCache
is a new implementation which now generalizes the availability of caching across other types of applications as well e.g. console application, winform applications etc. MemoryCache
is stored w.r.t. the application domain in which its instance was created and is application to all the users accessing the application. Quoting from this MSDN link:
The main differences between the Cache and MemoryCache classes are
that the MemoryCache class has been changed to make it usable by .NET
Framework applications that are not ASP.NET applications. For example,
the MemoryCache class has no dependencies on the System.Web assembly.
Another difference is that you can create multiple instances of the
MemoryCache class for use in the same application and in the same
AppDomain instance.
MemoryCache
class is present in a separate assembly System.Runtime.Caching.dll
altogether which can be referenced
Note: The MemoryCache
class and System.Web.Caching.Cache
class are different implementations lying in different dlls with no interdependency. It is just that conceptually their behaviors look very similar as anyways they are cache at the end of the day.
I would suggest reading this, this and this thread for even better understanding and some great thoughts on this topic.
To answer your question:
- To store anything which is application wide but light-weight - Use Application State.
- To store anything which is application wide but resource intensive - Use Web Cache
- To store anything which is user specific (usually light weight stuff as heavy weight stuff will not scale with growing users of your website) - Use Session state
As long as you are doing website development, the older web cache should be able to fulfill all your use-cases. There can be very specialized use cases in a weebsite where you would require the newer MemoryCache
but I can't think of any at the moment.