Keep data between sessions

Asked

Viewed 433 times

10

I have an ASP.Netet MVC project where I file a report that requires quite resource of the machine in question of processing.

I have a Action that generates and another that returns the report data in an Object List, so I thought that by creating a static variable, I could save the report data and return it when needed.

private static List<MeuObjeto> DadosRelatorio = new List<MeuObjeto>();
public ActionResult MinhaAction ()
{
    // Limpando dados que já estavam.
    if (DadosRelatorio.Count() > 0)
    {
        DadosRelatorio.Clear();
    }

    ...
    // Preenchendo DadosRelatorio
    ...
}
public ActionResult OutraAction ()
{
  return Json(DadosRelatorio, JsonRequestBehavior.AllowGet);
}

The problem is that when 2 or more users simultaneously generate a report, the data of one overwrites the data of another.

I thought that by creating a static variable itself framework would manage a variable for each one on the server, but that’s not what happens, the same variable is for all users.

Is there any alternative?

2 answers

10


In fact the static variable cannot be used. You need to create a session-based cache system or at least keep the information within the same session.

public ActionResult MinhaAction() {
    Session["relatorioX"] = MontaRelatorio();
    ...
}
public ActionResult OutraAction() => Json(Session["relatorioX"], JsonRequestBehavior.AllowGet);

I put in the Github for future reference.

The MontaRelatorio() probably returns a List<MeuObjeto>.

This is obviously a simplification, errors can occur if used like this but does what you were doing keeping the data per session instead of keeping it per application instance.

I believe in this case you can use TempData instead of Session hassle-free.

I do not know if this meets all needs but solves the problem according to what was specified in the question.

I would avoid putting a large amount of data between sessions but it’s a simple solution.

That one response in the OS has a cached solution. Other using the cache system itself framework.

ASP.NET Core has one new cache system more flexible (example of use).

  • is a great solution, but really have a lot of data to store, it would cause me problems?

  • 1

    It may make the system a little slow but it’s obviously a substantial gain from generating everything all over again. In fact you may not even notice performance problems. test and tell me how you did.

  • I realized that the TempData is "alive" for a little while, I think Session would be the only alternative to solve that same problem. Thank you very much for the help.

  • 1

    Yeah, it doesn’t solve if you need to keep it longer.

  • @Jedaiasrodrigues Another Alternative is to use a document cache, such as Couchbase.

  • Creates a session system in the database, creates a unique variation that identifies that session and the data in question

Show 1 more comment

1

A suggestion: use different names for reports, based on user identifiers (if applicable), where you can, through a BATCH running 1x a day, delete older reports based on their dates (if applicable).

This way Voce will have "user-dependent" reports (increasing disk use obviously), without relying exclusively on perennial variables.

  • 3

    This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication.

  • Forgive me, but my post offers an alternative solution, within the larger context he wants: offer a non-permanent method of maintaining reporting data. Similarly, my tip is not a criticism or request for clarification. Therefore, I believe this is the right way to offer solutions - not necessarily the solution that, the way he is doing and others have clarified, will not be fulfilled.

Browser other questions tagged

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