【问题标题】:How can I use cache in a asp.net (vb.net framework 2) class?如何在 asp.net (vb.net framework 2) 类中使用缓存?
【发布时间】:2013-09-16 17:34:49
【问题描述】:

我在一个类中有一个方法,用于从 mssql 数据库中检索一些数据。根据用户的许可,我在许多页面和 Web 处理程序中使用它。我并不总是想从数据库中获取数据,而是需要利用缓存的优势。我正在使用 vb.net 框架 2 作为我的客户端并且不想更改它。我尝试了很多方法,但没有奏效。谁能帮帮我...

我最后一次尝试是这样的:

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

这是一个类(testClass.vb)中的方法之一,有什么建议请...

【问题讨论】:

  • 也许如果您详细说明“无效”在症状方面的确切含义,人们可能会提供建议。
  • 你用什么方法执行这段代码?
  • 感谢扩展的建议,我在 web 处理程序和一些普通的 aspx 页面中使用了这个类,我正在 web 处理程序中对其进行测试并调用此方法来显示数据。我预计第二次缓存的结果,但它进入“如果条件”(意味着没有缓存的数据集),我是否使用正确的方式将数据集存储在缓存中?我使用了这个“Web.HttpContext.Current.Cache.Insert("myData", myDataSet, Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero)"

标签: c# asp.net .net vb.net


【解决方案1】:

在尝试将其分配给 DataSet 之前,您应该检查 Nothing 的缓存值,例如

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多