PassionateDeveloper
PassionateDeveloper

Reputation: 15138

How to cache global information for all users?

I have my first bigger asp.net website and there are userlists of all user online - of course this list is the same for every user, but as a normal online list I update this with PageMethod / WebMethod every 10 seconds. So if 100 users online that means 10x6x100 = 6000 database querys each minute.

How can I avoid that? Can I save this information for all user in something like a session / querystring / cookie but global for all users to avoid querys?

Upvotes: 0

Views: 86

Answers (2)

Ratna
Ratna

Reputation: 2319

You may us static variable for this. If you are having more than 1 app-pool to serverpages then use asp.net caching since static variable are not thread safe.

Here is my code that i use for something similar it has 2 class. class1

  using System;

   public class onlineuser
   {
     public string sessionid = "";
     public string username = "";
     public string currentpage = "";
     public DateTime time = DateTime.Now;
public onlineuser()
{
    //
    // TODO: Add constructor logic here
    //
}

}

class2

 using System;
 using System.Collections;
 using System.Data;


 public class user
 {
 public static ArrayList online;

 public static void adduser(string sessionid,string username,string currentpage)
 {
      removeunused();
      remove(sessionid);
      onlineuser ou = new onlineuser();
      ou.sessionid = sessionid;
      ou.username = username;
      ou.currentpage = currentpage;
      ou.time = DateTime.Now;
      if (online==null)
      {
           online = new ArrayList();
      }
      online.Add(ou);
      online.TrimToSize();

 }
 public static void remove(string sessionid)
 {
      if (online==null)
      {
           return;
      }
      onlineuser ou = new onlineuser();
      for (int i = 0; i < online.Count; i++)
      {
           ou = (onlineuser)online[i];
           if (ou.sessionid == sessionid)
           {
                online.RemoveAt(i);
                online.TrimToSize();
                return;
           }
      }
 }
 public static void removeunused()
 {
      if (online == null)
      {
           return;
      }
      onlineuser ou = new onlineuser();
      for (int i = 0; i < online.Count; i++)
      {
           ou = (onlineuser)online[i];
           if (ou.time < DateTime.Now.AddMinutes(-2))
           {
                online.RemoveAt(i);
                online.TrimToSize();
                return;
           }
      }
 }
 public static DataTable totable()
 {
      DataTable dt = new DataTable();
      DataColumn dc = new DataColumn("SessionId", typeof(string));
      DataColumn dc1 = new DataColumn("UserName", typeof(string));
      DataColumn dc2 = new DataColumn("currentpage", typeof(string));
      DataColumn dc3 = new DataColumn("Time", typeof(DateTime));
      dt.Columns.Add(dc);
      dt.Columns.Add(dc1);
      dt.Columns.Add(dc2);
      dt.Columns.Add(dc3);
      if (online!=null)
      {
           onlineuser ou = new onlineuser();
      for (int i = 0; i < online.Count; i++)
      {
           ou = (onlineuser)online[i];
          dt.Rows.Add(new object[] {ou.sessionid,ou.username,ou.currentpage,ou.time});
      }
      }
      return dt;
 }

}

following code is placed in mymaster page which update userlist

   try
     {
          string uname= "N/A";
     if (Session["uname"]!=null)
     {
          uname = Session["uname"].ToString();
     }
     string page = Path.GetFileName(Request.PhysicalPath).Trim().ToLower();
     if (Request.QueryString!=null)
     {
          page += "?"+Request.QueryString.ToString();
     }
     user.adduser(Session.SessionID, uname, page);
     }
     catch (Exception)
     {


     }

Upvotes: 0

Kinjan Shah
Kinjan Shah

Reputation: 76

The Simplest way is to create an Application Variable or DataTable, which will hold your Required Information.

After each 10 minutes, when you update the records, Just update the Application Datatable you created above. This DataTable is common for all the users and that will decrease your load drastically.

Let me know if you need code.

Upvotes: 1

Related Questions