Array Caching

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

I would like to cache a list of names of the last 5 users in server cache.
and when a new user visits this page it would delete the oldest user in the
last and add the new user.

pseudo code

string[] actUserList;
actUserList[] = (string[])Cache[("AUL")];
//delete oldest user code


Cache.Insert ("AUL", actUserList[] + getUsername());

String actUserListNew[] = (string[])Cache[("AUL")]; //updated active user
list

Thanks,

Aaron
 
Ok, I understand what you want. What I don't understand is what your problem
is. Do you want an object that will maintain a list of 5 (or a configurable
number) user names?
 
My problem is I don't know how to write this code. :) im new to c#.
is. Do you want an object that will maintain a list of 5 (or a configurable
number) user names?
Yes, a list of 5

thanks in advance
 
See is this works for you...
_________________________________________
public class UserList{
private int maxCount;

private Queue userQueue;


private UserList()

{

}


public UserList(int maxCount)

{

this.maxCount = maxCount;

userQueue = new Queue(maxCount);

}

public void Add(string userName)

{

lock(userQueue.SyncRoot)

{

if (userQueue.Count == maxCount)

userQueue.Dequeue();

userQueue.Enqueue(userName);

}

}

public string[] GetUsers()

{

string[] users = new string[userQueue.Count];

lock(userQueue.SyncRoot)

{

int i = 0;

foreach(object item in userQueue)

{

users = item.ToString();

i ++;

}

}

return users;

}

}

_______________________________________
 
Sorry, I didn't intend to send the previous message...

The object needs to be in "global" scope so only one instance is ever
created and so there is only *one* list of users. You should probably use
the object tag in global.asax for this

<object id="ListOfUsers" runat="server" class="YourAppNamespace.UserList"
scope="Application" />

You can then refer to this object (in your Pages) as ListOfUsers.

It's possible that the class UserList will need to be a singleton for this
situation. Please note that I've only tested the UserList class in a
non-threaded environment to make sure it works as expected. I have not
tested it in an ASP.NET environment. I've made the class thread-safe (as far
as I know). I'm new to C# and ASP.NET myself :)

all the best.
 
Back
Top