Cache or class variables?

Is it better to use class variables or session cache in ASP.NET? I have a variable that I need to keep the pages loaded. Better to use:

public class MyClass {
  private SomeObject anObject;
  private method1() {
    anObject = new SomeObject();
  }
  private method2() {
    // do something with anObject
  }
}

      

or

public class MyClass {
  private method1() {
    Session["anObject"] = new SomeObject();
  }
  private method2() {
    SomeObject anObject = (SomeObject)Session["anObject"];
  }
}

      

0


source to share


1 answer


Use cache. I am not working on .NET, but in Java with servlets. There I would definitely use an HTTP session since my application can run on a cluster of servers and I know that storing a value in an HTTP session will work in this setup, while storing it in a class variable can be problematic. You can store the entire MyClass class in the HTTP session and then you can store the values ​​in class variables.



Basically, you can view HTTP sessions as a hash table.

+2


source







All Articles