[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webservices

Re: IP based session variables...

Nathan Baulch

7/18/2003 1:15:00 PM

> I'd rather not have to create my own static Hashtable of Hashtables
> if there is a switch somewhere.

This proved to be easier than I thought.
Can anybody see any issues or suggest any optimizations in my implementation
below?


public class Utils {
public static Hashtable Session {
get {
string key = "Session_" +
HttpContext.Current.Request.UserHostAddress;
Hashtable session = (Hashtable)HttpContext.Current.Cache[key];
if(session == null) {
session = new Hashtable();
HttpContext.Current.Cache.Insert(

key,session,null,DateTime.MaxValue,TimeSpan.FromHours(1));
}
return session;
}
}
}


Nathan


2 Answers

Marina

7/18/2003 6:18:00 PM

0

This isn't the exact equivalent of session though. Your session_start and
sesion_end events won't fire in the same way. Also, how will you clean up
objects after the timeout has been reached? If you don't, this hashtable
will get huge over time...


"Nathan Baulch" <nathan.baulch@microsell.com.au> wrote in message
news:eQm5C6STDHA.1688@TK2MSFTNGP11.phx.gbl...
> > I'd rather not have to create my own static Hashtable of Hashtables
> > if there is a switch somewhere.
>
> This proved to be easier than I thought.
> Can anybody see any issues or suggest any optimizations in my
implementation
> below?
>
>
> public class Utils {
> public static Hashtable Session {
> get {
> string key = "Session_" +
> HttpContext.Current.Request.UserHostAddress;
> Hashtable session = (Hashtable)HttpContext.Current.Cache[key];
> if(session == null) {
> session = new Hashtable();
> HttpContext.Current.Cache.Insert(
>
> key,session,null,DateTime.MaxValue,TimeSpan.FromHours(1));
> }
> return session;
> }
> }
> }
>
>
> Nathan
>
>


Nathan Baulch

7/19/2003 3:09:00 AM

0

> Your session_start and
> sesion_end events won't fire in the same way. Also, how will you clean up
> objects after the timeout has been reached?

That's why I've implemented it as a Cache of Hashtables and not a Hashtable
of Hashtables. Each IP is allocated a Hashtable to store variables that
expires in 1 hour. That way I don't need Session_Start and Session_End
because it's self maintaining.

This works well because I want session variables to be accessible between
browser sessions even if one is closed.


Nathan