Objectcache to provide a variable for all users

Asked

Viewed 357 times

2

I have an application in ASP.NET and would like to cache for all users as if the variable was in pool application and everyone had access, something other than the session that makes the section variable for each user. ObjectCache would be the most appropriate?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

4 answers

3

What is good for you in the specific situation only you can say. Even though you put too many details it can still be difficult to tell precisely what is best because any detail omitted, even through ignorance of it, may change the recommendation.

It seems you are on the right track. I do not know if it is the best but use an implementation of ObjectCache is something that will get the desired result, since done correctly, of course.

Just pay attention to the detail that this is an abstract class, so you’ll have to use a concrete implementation of it or create your own implementation. You probably want to use an existing one and it looks like you want to do it in memory. So in practice you will use the MemoryCache. Then your entire application will work within the protocols of this class to handle the cache. Which is good because it was designed by professionals who understand the subject and know how to make it work the best way. You should only do your own implementation if you really need something specific that another doesn’t answer and know what you’re doing. Even in this case, use the ObjectCache as a basis will still be desirable.

It’s nice to see that you’re avoiding other more specific forms of caching like System.Web.Caching.Cache which depends on the "classic" ASP.NET which was a dependency of ASP.NET MVC but is no longer in the newer versions and the ideal is to use nothing that is not "universal" from . NET.

There are other ways to create a cache in ASP.NET MVC but it depends on what you are doing. Many are very specific. There are options where you notes the cache in properties defining in the controller. Has a framework of third parties with several options for each need.

Remembering that the . NET Core is now preferable.

2


This is an example of the use of objectcache the variable is available to everyone in the application pool:

if (cache[Cache.Key.PesquisaGrupoProjetos.ToString()] != null)
    grupos = ((IList<EstruturaSIIMDTO>)cache[Cache.Key.PesquisaGrupoProjetos.ToString()]);
else
{
    grupos = db.Get(new ConsultaGrupoProjeto()).ToList();
    cache.Set(Cache.Key.PesquisaGrupoProjetos.ToString(),grupos,DateTime.Now.AddMinutes(720));
}

2

If you intend your code to be scalable, then the best case is to use a database.

Static data into a web application in the memory of the process serving a request is not good practice. Unless you intend to implement a ObjectCache who read and write in the comic book yourself, you will be limited to MemoryCache which does exactly what I said earlier. (There is an implementation of ObjectCache distributed call NCache, read further below).

  • What happens when there is more than one process serving requests?

  • What happens if there is more than one server behind a load balancer? This happens on the Azure platform for example.

But then you could argue that the database is slow... but the truth is it’s not. Most applications read from the database, in all requests. In addition, it is possible to use multilevel cache strategies: using memory and database at the same time, when the need arises.

The point is... why not start with the safest? That works in all cases, and then the measure necessary to optimize the cache, migrate to a mixed approach, install a fiber optic cable between the web server and the BD server, migrate to Azure, Amazon, etc... are so many options to optimize this.

Distributed cache technologies

They are the best options as they are made to work even in distributed environments.

2

I strongly recommend using a Cache service such as Redis that Azure Cache uses.

This way you ensure that the cache will coexist even in scalable environments, and also does not mix responsibilities, such as using the application database as a temporary repository of your cache.

See more about Azure Cache.

See more about Cache Redis.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.