0

I have website with array (list) of 1000 objects, these objects are loading from json to array every website refresh. I would like to load these objects from json to array only once and keep it in RAM for others users. Because everytime read file is much slower than read it from RAM.

I am using ASP.NET Web Forms

How is it posssible?

Tim
  • 4,051
  • 10
  • 36
  • 60
tomsk
  • 967
  • 2
  • 13
  • 29

2 Answers2

0

I would recommend to define the array as an static member of a class and then initialize it with help of Global.asax, use the Application_Start event handler.

to add Global.asax to you project in Visual Studio:

File -> New -> File -> Global Application Class

Here is a sample C# code for Global.asax.cs:

public class Global : HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        //  ... Your initialization of the array done here ...
    }

    protected void Session_Start(object sender, EventArgs e)
    {
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
    }

    protected void Application_Error(object sender, EventArgs e)
    {
    }

    protected void Session_End(object sender, EventArgs e)
    {
    }

    protected void Application_End(object sender, EventArgs e)
    {
    }
}
ialiashkevich
  • 615
  • 7
  • 8
0

Are these values static, i.e., do they stay constant while your application is running? In that case, the easiest way is to cache those values.

You can use static variables for that, but the recommended way is to use the thread-safe Cache object provided by ASP.NET. It can be accessed with the Cache property of the Page or of the HttpContext.

Example:

var myList = (MyListType)Cache["MyList"];
if (myList == null)
{
    myList = ...;              // Load the list
    Cache["MyList"] = myList;  // Store it, so we don't need to load it again next time.
}

Further reading:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • You need to include a reference to System.Runtime.Caching to enable this. – Tim Feb 18 '16 at 20:49
  • @Tim: Are you sure we are taking about the same thing? The [Cache class I am talking about](https://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.110).aspx) lives in the System.Web assembly. – Heinzi Feb 18 '16 at 20:53
  • I just implemented this in an MVC 4 site and needed System.Runtime.Caching...I didn't have it off-the-shelf in my project. Maybe decisions from developers before me... – Tim Feb 18 '16 at 20:56
  • Ah, there's the difference: This question is about WebForms, and you are using MVC. The System.Web assembly contains all the WebForms stuff. – Heinzi Feb 18 '16 at 21:00
  • what is difference between your post and ialiashkevich's post? Because ialiashkevich's post works quite well – tomsk Feb 19 '16 at 12:28
  • @tomsk: He uses a static variable, with all the pro and cons mentioned in the linked question. – Heinzi Feb 19 '16 at 13:21