0

I'm working on a project with Web API 2 and I'm trying to find the best way to save the user configuration in memory.

Each user has a particular configuration (timezone, location, language, its company information, and many more information), so I'm trying to implement a way to query this information at first login and save it to memory or something. So this information is frequently used by many operations and I don't want to slow down the application performance by querying all that info each time I need it.

So, the first plan was to implement a Static clas with this information, but I don't know if it's the best approach.

Can someone suggest the best way to implement this on a Web API 2?

Juan Jose Adan
  • 135
  • 1
  • 1
  • 5
  • 1
    If your services are intended to be RESTful (per the `rest` tag), adding server-side session state breaks that. –  Mar 22 '18 at 16:00
  • 1
    You could use `HttpContext.Current.Cache`. E.g. see [this answer](https://stackoverflow.com/a/18907961/1220550) – Peter B Mar 22 '18 at 16:01
  • 1
    you can use redis cache – Hussein Salman Mar 22 '18 at 16:02
  • In memory hosting will be helpful in this case: https://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/ – Agnel Amodia Mar 22 '18 at 16:49
  • 1
    Depending on the amount of data, you might find claims-based authentication suitable. The data would be encoded in a token that is part of each API request. – Ian W Mar 22 '18 at 17:38

2 Answers2

0

You can store all additional information in claims identity.

Sunil Shrestha
  • 303
  • 1
  • 7
0

You can add the information in session:

Try with: In the Global.asax you must add the next code:

protected void Application_PostAuthorizeRequest()
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}

And now, you are available to use Session in your controllers or classes:

var session = System.Web.HttpContext.Current.Session;
session["token"] = "abc";

And the variable session would persist in the application and get the session values

var abc = session["token"].ToString();

Reference: How to keep alive a variable among a class inherit from ApiController and other one inherit from ExceptionFilterAttribute

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157