users online function query

P

Paul

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
 
P

Paul

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)
{...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top