简体   繁体   中英

How can I use cache in a asp.net (vb.net framework 2) class?

I have a method, in a class, for retrieving some data from mssql database. I use this in many pages and web handlers depending on user's permission. I don't always want get data from database and need to use the advantage of caching. I am using vb.net framework 2 as my client and don't want to change it. I tried in many ways, but it didn't work. Can anyone help me...

My last try was this:

Dim myDataSet As New DataSet
myDataSet = CType(Web.HttpContext.Current.Cache.Get("myData"), DataSet)

If myDataSet Is Nothing Then
    myDataSet = GetData("select * from Table")
    Web.HttpContext.Current.Cache.Insert("myData", myDataSet, Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero)
    'End If
End If

This is one of the methods from a class (testClass.vb), Any suggestions please...

You should check Cache value for Nothing before trying to assign it to DataSet, something like

Dim myDataSet As DataSet

If HttpContext.Current.Cache.Get("myData") Is Nothing

    myDataSet = GetData("select * from Table")
    HttpContext.Current.Cache.Insert("myData", myDataSet, Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero)

Else

    myDataSet = HttpContext.Current.Cache.Get("myData") 

End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM