Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing variable from Cache

Status
Not open for further replies.

lunargirrl

Programmer
Jan 26, 2004
77
BR
I am creating a cache in this way:
'I found this code in the net and it is working really fine, but i have a problem: how can i remove this variables from my cache if the user clicks the "close" button in my application? (I mean the sintax to remove the str1 from cache)

here's my code:

on my login page
---------------------

Dim str1 As String = username & userid
Dim struser As String = Convert.ToString(Cache(str1))

If struser Is Nothing Or struser = [String].Empty Then
Dim SessTimeOut As New TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0)

HttpContext.Current.Cache.Insert(str1, str1, Nothing, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.Normal, Nothing)

Session("user") = username & userid

'then autenticate...
else
labe1.text = "Login Failure"
endif

on my global.asax
------------------------
Protected Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
If Not (Session("user") Is Nothing) Then
Dim str1 As String = CStr(Session("user"))

Dim struser As String = CStr(HttpContext.Current.Cache(str1))
End If
End Sub

TIA,
Gis.
 
Hi people, someone could help me about this?

Thanks,
Gis.
 
The problem is if the user clicks the close button (by this I mean the X in the top right of the browser) there is no postback to the server and hence there is no way for you to clear the cache. Sorry. The syntax would be
Code:
Cache.Remove(str1);
I noticed you are using the same string (str1 in this case)for the key and value for your cache item. This means you have to know the value of the item in the Cahce to retrieve it which kind of defeats the object of putting the value in the Cache in the first place doesn't it?

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
crazyboybert, thanks for your answer.


as i noticed the problem is to clear the variable struser somehow:

If struser Is Nothing Or struser = [String].Empty Then

The problem is, my "close" button (not the browser button X) is in another webform, as vb6 programmer i could declare struser as global variable, and work with it in various forms, but i dont find a way to make it as global variable in asp.net.

the same i would do with the instruction Cache.Remove(str1), i have to declare str1 as global, because I am cleaning from another webform.

Is there a way to do that?

Thanks a lot
Gis.
 
I can't really see what your trying to achieve here? Both the Session and Cache objects are global within your appplication and persist over postbacks. Either of these is suitable for holding a "global" variable.

As i said in my previous response the way you are using cache doesn't make sense. By having the string str1 as the key and the value you need to know the value of str1 to retrieve its value from the Cache. This is using memory for the sake if it. If you wanted to store the username in cahce it would be more sensible to do something like this
Code:
HttpContext.Current.Cache.Insert("somekey", str1, Nothing, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.Normal, Nothing)
And then retrieve it with
Code:
Dim strUser = Cache("somekey") As String
However this is not user specific so if you wanted to store username like this it wouyld be better to use Session as you are. I just can't see why you need the Cache object in this scenario or what it is doing for you?

Rob


Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top