【问题标题】:Caching on web services in vb.net在 vb.net 中缓存 Web 服务
【发布时间】:2016-09-22 07:33:10
【问题描述】:

我想在我的所有 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

【问题讨论】:

    标签: c# .net vb.net web-services


    【解决方案1】:

    我对我的方法进行了一些更改,现在缓存工作! 我使用 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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2011-01-18
    • 2015-07-18
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多