In my web application I need to cache some data as they are required frequently but changes less often. To hold them I have made a sepearate static class which hold these field as static values. These field get initialized on first call. see a sample below.
public static class gtu
{
private static string mostsearchpagedata = "";
public static string getmostsearchpagedata()
{
if (mostsearchpagedata == "")
{
using (WebClient client = new WebClient())
{
mostsearchpagedata = client.DownloadString("https://xxx.yxc");
}
}
return mostsearchpagedata;
}
}
Here webrequest is only made one time, it works ok but if they are called in quick succession when there are large no. of users and apppool has restarted, webrequest is made multiple times depending upon mostsearchpagedata was initialized or not.
How can I make sure that webrequest happens only one time, and all other request wait till the completion of the first webrequest?