I want to build an MVC application that replaces the session object with the caching mechanism of .net framework 4.0.
The application has to store some values in some kind of cache to keep the database traffic and the loading times low (e.g. settings, dropdown values, etc.). The users authenticate themselfs with a username and a password. The default FormsAuthentication is used here. My idea was to create a unique MemoryCache for each user and a general MemoryCache for the application.
My getter for the User MemoryCache looks like this
private MemoryCache _memCache
{
get
{
if(HttpContext.Application[User.Identity.Name] == null)
{
HttpContext.Application[User.Identity.Name] = new MemoryCache(User.Identity.Name);
}
return HttpContext.Application[User.Identity.Name] as MemoryCache;
}
}
If a user logs out the cache will be disposed.
This is the global MemoryCache for the application
private MemoryCache _appMemCache
{
get
{
if(HttpContext.Application["ApplicationMemoryCache"] == null)
{
HttpContext.Application["ApplicationMemoryCache"] = new MemoryCache("ApplicationMemoryCache");
}
return HttpContext.Application["ApplicationMemoryCache"] as MemoryCache;
}
}
The question is if the Application object is usable to store the MemoryCache. And if the CacheItemPolicy works without problems with this.
The reason for me not to use the session is the possible timeout and the blocking of parallel request.