V
V
VanilaSpirit2020-09-08 18:23:25
ASP.NET
VanilaSpirit, 2020-09-08 18:23:25

ASP.NET how to cache variable in header?

Each time a request is made, a selection is made in the header again, and, accordingly, operations on the page are performed on it.

As an example:

@{
var users = context.Users.Where(x => x.Status != null).ToList();
}


I think with a large amount of data (I have about 300k) and 20+ requests per second, the execution speed on the server will noticeably drop. Is it possible to somehow cache this variable, or do some other trick with it so that it is not loaded once again? I thought about indexing it, but in fact it is always unchanged...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-09-08
@firedragon

Try something like this.
When the collection changes, call UserStatus.Instance.UserChanged(users);

public class UserStatus{
  public static UserStatus Instance => _instance;
  public IList<User> Users => getUsers(); 
  public void UserChanged( IList<User> users){
///
}
}

R
Roman, 2020-09-09
@yarosroman

https://docs.microsoft.com/en-us/aspnet/core/perfo... . I think it's a more reasonable solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question