users online function query

Discussion in 'ASP .Net' started by Paul, Jul 16, 2009.

  1. Paul

    Paul Guest

    Hi all,

    I've been trying to create a control that is found on most common
    forums: displaying users and guests online.

    My original way of doing this was to have a static class as follows:

    private static Object objLock = new Object();

    public static void NewUser(string _sessionID, bool
    _IsLoggedIn, string _username, string _IP)
    {
    private static
    System.Collections.Generic.Dictionary<string, onlineUser> onLineUsers

    = new Dictionary<string, onlineUser>();

    lock (objLock)
    {
    if (!onLineUsers.ContainsKey(_sessionID))
    {
    onLineUsers.Add(_sessionID, new onlineUser(false,
    "guest", _IP));
    }
    }
    }

    public static string GetOnlineUsers(string _role)
    {
    lock (objLock)
    {
    //do work
    }
    }
    //...
    }

    online user is a small class with members such as:

    class onlineUser
    {
    public onlineUser(bool _IsLoggedIn, string _username, string
    _IP)
    {
    IsLoggedIn = _IsLoggedIn;
    username = _username;
    updated = DateTime.Now;
    IP = _IP;
    role = "guest";
    lastPageViewedTitle = string.Empty;
    lastPageViewedUrl = string.Empty;
    hideOnlineStatus = false;
    }

    public bool IsLoggedIn;
    public string username;
    public DateTime updated;
    public string IP;
    public string role;
    public string lastPageViewedTitle;
    public string lastPageViewedUrl;
    public bool hideOnlineStatus;
    }

    Please note this is not full code, so excuse public members etc.

    So on session creation a new onlineUser is added to onLineUsers.
    Each page calls GetOnlineUsers and other methods (not shown) deal with
    users logging in, session ending (using inProc so this is ok) etc.

    objLock is used to ensure thread safety.

    My concern is that creating the functionality using static methods
    will lead to a bottleneck, as only 1 request at a time will be able to
    process onLineUsers or related functions.

    Other options include using the DB - but I reason that once again, due
    to the nature of the function, table locking will still see such a
    bottleneck but with added expense of every page request of every user
    hitting the db.
    I could use Application, but once again, the bottle neck will occur.

    With this in mind, would using the static method be the lesser of all
    evils? Or am I missing something here?
    I reasoned cache was out of the question as the data held in
    onLineUsers will be updated frequently - making it a bad candidate for
    cache.

    Any advice would be appreciated.

    Paul
    Paul, Jul 16, 2009
    #1
    1. Advertising

  2. Paul

    Paul Guest

    On 16 July, 17:21, Paul <> wrote:
    > Hi all,
    >
    > I've been trying to create a control that is found on most common
    > forums: displaying users and guests online.
    >
    > My original way of doing this was to have a static class as follows:
    >
    > private static Object objLock = new Object();
    >
    >         public static void NewUser(string _sessionID, bool
    > _IsLoggedIn, string _username, string _IP)
    >         {
    >             private static
    > System.Collections.Generic.Dictionary<string, onlineUser> onLineUsers
    >
    > = new Dictionary<string, onlineUser>();
    >
    >             lock (objLock)
    >             {
    >                 if (!onLineUsers.ContainsKey(_sessionID))
    >                 {
    >                     onLineUsers.Add(_sessionID, new onlineUser(false,
    > "guest", _IP));
    >                 }
    >             }
    >         }
    >
    >         public static string GetOnlineUsers(string _role)
    >         {
    >             lock (objLock)
    >             {
    >                      //do work
    >              }
    >         }
    > //...
    >
    > }
    >
    > online user is a small class with members such as:
    >
    > class onlineUser
    >     {
    >         public onlineUser(bool _IsLoggedIn, string _username, string
    > _IP)
    >         {
    >             IsLoggedIn = _IsLoggedIn;
    >             username = _username;
    >             updated = DateTime.Now;
    >             IP = _IP;
    >             role = "guest";
    >             lastPageViewedTitle = string.Empty;
    >             lastPageViewedUrl = string.Empty;
    >             hideOnlineStatus = false;
    >         }
    >
    >         public bool IsLoggedIn;
    >         public string username;
    >         public DateTime updated;
    >         public string IP;
    >         public string role;
    >         public string lastPageViewedTitle;
    >         public string lastPageViewedUrl;
    >         public bool hideOnlineStatus;
    >     }
    >
    > Please note this is not full code, so excuse public members etc.
    >
    > So on session creation a new onlineUser is added to onLineUsers.
    > Each page calls GetOnlineUsers and other methods (not shown) deal with
    > users logging in, session ending (using inProc so this is ok) etc.
    >
    > objLock is used to ensure thread safety.
    >
    > My concern is that creating the functionality using static methods
    > will lead to a bottleneck, as only 1 request at a time will be able to
    > process onLineUsers or related functions.
    >
    > Other options include using the DB - but I reason that once again, due
    > to the nature of the function, table locking will still see such a
    > bottleneck but with added expense of every page request of every user
    > hitting the db.
    > I could use Application, but once again, the bottle neck will occur.
    >
    > With this in mind, would using the static method be the lesser of all
    > evils? Or am I missing something here?
    > I reasoned cache was out of the question as the data held in
    > onLineUsers will be updated frequently - making it a bad candidate for
    > cache.
    >
    > Any advice would be appreciated.
    >
    > Paul


    oops, I somehow failed at copying+pasting. The top of the class looks
    like this:

    public static class WhoseOnline
    {/
    private static System.Collections.Generic.Dictionary<string,
    onlineUser> onLineUsers
    = new Dictionary<string, onlineUser>();

    private static Object objLock = new Object();

    public static void NewUser(string _sessionID, bool
    _IsLoggedIn, string _username, string _IP)
    {...
    Paul, Jul 16, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sunil Miriyala
    Replies:
    0
    Views:
    737
    Sunil Miriyala
    Mar 1, 2004
  2. nani
    Replies:
    0
    Views:
    362
  3. chinni
    Replies:
    0
    Views:
    368
    chinni
    Nov 20, 2009
  4. chinni
    Replies:
    0
    Views:
    425
    chinni
    Nov 20, 2009
  5. chinni
    Replies:
    0
    Views:
    448
    chinni
    Nov 20, 2009
Loading...

Share This Page