簡體   English   中英

在vb.net中緩存Web服務

[英]Caching on web services in vb.net

我想在所有Web服務的Web方法中添加緩存。 但是我不知道正確的方法...

有什么想法我可以做什么? 謝謝 !

這是我的代碼..

Dim _cache As Cache = New Cache()
<WebMethod>
Public Function GetAllSpeakers(SubSite As String, MyUsername As String, MyPassword As String) As String
    If ValidateUser(MyUsername, MyPassword) Then

        Dim cachedSpeakers As String
        cachedSpeakers = CStr(_cache("CacheSpeakers"))

        If (cachedSpeakers Is Nothing) Then
        Try
            Dim passWordEnc As SecureString = New SecureString()
            For Each c As Char In Password.ToCharArray()
                passWordEnc.AppendChar(c)
            Next

            Dim subContext As ClientContext = New ClientContext("url")
            subContext.Credentials = New SharePointOnlineCredentials(Username, passWordEnc)

            Dim json As String = JsonConvert.SerializeObject(LibraryMethods.Methods.GetSpeakers(subContext))

            _cache.Insert("CacheSpeakers", json,
            Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration,
            New TimeSpan(0, 30, 0))

            Return json

        Catch ex As Exception
            Return "ERROR: " & ex.InnerException.ToString
        End Try
    Else
        Return _cache("CacheSpeakers").ToString()
        End If
    Else
        Return "AUTHENTICATION FAILED"
    End If
End Function

我對我的方法進行了一些更改,現在可以正常工作了! 我使用HttpRuntime.Cache。

<WebMethod>
Public Function GetAllSpeakers(SubSite As String, MyUsername As String, MyPassword As String) As String
    If ValidateUser(MyUsername, MyPassword) Then

        If (HttpRuntime.Cache("CacheSpeakers") Is Nothing) Then
            Try
                Dim passWordEnc As SecureString = New SecureString()
                For Each c As Char In Password.ToCharArray()
                    passWordEnc.AppendChar(c)
                Next

                Dim subContext As ClientContext = New ClientContext("url")
                subContext.Credentials = New SharePointOnlineCredentials(Username, passWordEnc)

                Dim json As String = JsonConvert.SerializeObject(LibraryMethods.Methods.GetSpeakers(subContext))


                HttpRuntime.Cache.Insert("CacheSpeakers", json, Nothing,
                Cache.NoAbsoluteExpiration, New TimeSpan(0, 30, 0),
                CacheItemPriority.NotRemovable, Nothing)

                Return json

            Catch ex As Exception
                Return "ERROR: " & ex.InnerException.ToString
            End Try
        Else
            Return HttpRuntime.Cache("CacheSpeakers").ToString()
        End If
    Else
        Return "AUTHENTICATION FAILED"
    End If
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM