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!

Learning Data Cache 1

Status
Not open for further replies.

bitseeker

Programmer
Nov 27, 2005
60
US
I'm self-traning on ASP.NET and am using the code below to figure out data caching. It gets several errors...how
should I rewrite it to make it work? (I expect I'm making several incorrect assumptions here about scope, etc... hopefully in the process of figuring this example out I'll learn more about those things, too!)

Learning objective here is to put something in data cache on one page's event, and then get it out on another page's event...

Thanks!

'--------------------------------------
Partial Class Pages_AddCache
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AddCache As New AddCache
AddCache.Add()
End Sub
End Class
'--------------------------------------
Partial Class Pages_GetCache
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim GetCache As New GetCache
GetCache.Retreive()
End Sub
End Class

'---------------------------------------
Imports Microsoft.VisualBasic
Imports System.Web.Caching
Public Class AddCache
Public CC As New Cache
Sub Add()
CC("Test") = "Test" '<===System.NullReference Exception: Object reference
'not set to an instance of an object
msgbox("input " & CC("Test") )
End Sub
End Class

'---------------------------------------
Imports Microsoft.VisualBasic
Imports System.Web.Caching
Public Class GetCache '<=== seems either this overrides the public
' declare of CC above, or
Dim CC As Cache
Sub Retreive()
Dim aString As String = CC("Test") '<=== if not declared in this
' sub, says CC not declared
MsgBox("output " & aString)
End Sub
End
 
Thanks for the input, and the suggestion. I'm afraid I'm such a newbie that I need really explicit examples. The material in the "Help" and online MS didn't say anything about namespace declarations, or other context information, without which I'm struggling.

I did figure out a solution,.below. If you could provide specific suggestions on how to streamline it, that would be great.

Thanks!

'=======================
Type text into one page, get it out of another page.
'=======================
Partial Class Pages_AddCache
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AddCache As New AddCache
AddCache.Add(Me.TextBox1.Text)
End Sub

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

'=======================
Partial Class Pages_GetCache
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim GetCache As New GetCache
GetCache.Retreive()
End Sub
End Class

'=======================
Imports Microsoft.VisualBasic
Imports System.Web.Caching

Public Class AddCache
Public CC As Cache = HttpContext.Current.Cache
Sub Add(ByVal varString)
Dim aString As String = varString
CC("Test") = aString
'MsgBox("input " & CC("Test"))
End Sub
End Class
'=======================
Imports Microsoft.VisualBasic
Imports System.Web.Caching
Public Class GetCache
Dim CC As Cache = HttpContext.Current.Cache
Sub Retreive()
Dim aString As String = CC("Test")
MsgBox("output " & aString)
End Sub
End Class

'=======================

 
The cache can be as simple or as complicated as you want it. I write in c# and began using cache just recently. You can add objects to cache with the add or insert method.

Cache.Insert("THE NAME YOU WANT TO CALL IT", "WHATEVER OBJECT YOU WANT TO PUT IN CACHE");

WHen you reference it, just say

if(Cache["THE NAME YOU WANT TO CALL IT"] != null) {
DO THIS;
} else {
DO THIS INSTEAD;
Cache.Insert("THE NAME YOU WANT TO CALL IT", "WHATEVER OBJECT YOU WANT TO PUT IN CACHE");
}

By checking the cache for the name, you can use the cached object, if it is equal to null, it isn't in the cache, process what you need, and add that to the cache.

You can also set time expirations within the methods so if the current information changes or is update, your cached information won't remain and become obsolete.

Keep in mind the syntax above is C# and English...
 
Thanks for the input. I'm still trying to figure out, though, what all is needed in terms of the "environment" around the use of the Cache object.

In the example above, I couldn't get it to work without the

Imports System.Web.Caching and
Public CC As Cache = HttpContext.Current.Cache

Then it seems that I'm having to instantiate a pointer to HttpContext.Current.Cache called "Cache" (or "CC" in this case)in order use the Cache. (I'm getting that there is a lot of dim'ing and instantiating going on in .NET, made more intense by the weak persistence in ASP.NET.)

So, I'm wondering if there's a simpler way to reference Cache (or other "global" objects), than the way I did it.

Any guidance on this (including "Nope! Can't be done!) would be appreciated.

Thanks!
 
ca8msm said:
First of all, you don;t need to declare a new cache object. Secondly, try having a look at the Cache.Add method
I really don't know how I can be more specific than that! You don't have to create a new instance of Cache (which you have still persisted to do) as it's available throughout the application.

Then, either use Cache.Add or Cache.Insert to add items into the cache e.g.
Code:
Cache.Insert("myCacheEntry", myObject)




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for input. My apologies if I've misunderstood your post. I'm such a newbie, perhaps I'm flying way below the radar.

This
'--------------
Imports Microsoft.VisualBasic
Imports System.Web.Caching

Public Class Class3
Sub test()
Dim myObject As Class1
Cache.Insert("myCacheEntry", myObject)
End Sub
End Class
'---------------
Gives syntax check error "Reference to a non-shared member requires and object reference" on the Cache.Insert phrase.

So I seem to need more detail to get that format to work... (very newbie to ASP.NET, very different than VBA, lots of additional things to know, which I don't know until I know 'em :)
 
If you are referencing the cache from a page simply use:
Code:
        Dim myString As String = "test"
        Cache.Insert("myValue", myString)
        Response.Write(Cache.Item("myValue"))
If you are referencing it from inside a class use:
Code:
        Dim myString As String = "test"
        HttpContext.Current.Cache.Insert("myValue", myString)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for the input. That helps me understand the page versus class context in a general way, too.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top