1

如何在 VB.net 中实现 ServiceStack 缓存?我看过很多 C# 示例,但我无法将其转移到 vb.net。

我在第一个和第二个参数中得到堆栈的点ServiceStack.ServiceHost.RequestContextExtensions.ToOptimizedResultUsingCache

  • 第一个应该是:ServiceStack.ServiceHost.IRequestContext- 不确定 IRequestContext 是什么
  • 第二个应该是: ServiceStack.CacheAccess.Providers.MemoryCacheClient- 我如何在配置中设置这个做缓存默认值,即 MemoryCacheClient

下面的代码,任何建议都非常感谢。

全球.asax.vb

Public Class Global_asax
     Inherits System.Web.HttpApplication

     Public Class HelloAppHost
          Inherits AppHostBase

          Public Sub New()
               MyBase.New("Web Services", GetType(Wrapper).Assembly)
          End Sub

          Public Overrides Sub Configure(ByVal container As Container)

               Routes.Add(Of GetProduct)("/GetProduct").Add(Of GetProduct)("/GetProduct/{*}")

               Plugins.Add(New Cors.CorsFeature(allowedHeaders:="Content-Type, Authorization"))
               container.Register(Of ICacheClient)(New MemoryCacheClient())

          End Sub

     End Class


     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

          Dim apphost = New HelloAppHost()
          apphost.Init()

     End Sub

End Class

WS.vb

Public Class Wrapper

    Public Class WrapperGetProduct
        Implements IService(Of GetProduct)
        Public Function Execute(ByVal request As GetProduct) As Object Implements ServiceStack.ServiceHost.IService(Of GetProduct).Execute

            Dim cachekey As String = "some_key"
            Dim expireInTimespan = New TimeSpan(1, 0, 0)

            Return ServiceStack.ServiceHost.RequestContextExtensions.ToOptimizedResultUsingCache(
                ServiceStack.ServiceHost.IRequestContext,  // not sure what this should be
                ServiceStack.CacheAccess.Providers.MemoryCacheClient,  // not sure what this should be - how do I set this to cache setted in configuration (in memory cache)?
                cachekey, expireInTimespan, 
                Function() request.HandleRequest()
            )


        End Function
    End Class

End Class
4

1 回答 1

2

使用新的 API

ToOptimizedResultUsingCache是服务继承/公开的扩展方法RequestContext,与 Cache 相同(通过 IOC 自动解决)。

下面的示例从 C# 转换,缓存/包装现有服务(AppConfig 和 Repository 通过 IOC 解析,在 AppHost 配置方法中注册)。

Imports System.Collections.Generic
Imports ServiceStack.Common
Imports ServiceStack.ServiceHost
Imports ServiceStack.ServiceInterface.ServiceModel
Imports ServiceStack.Common.Web

Public Class SearchTerm

    Public Property Latitude() As Double
        Get
            Return m_Latitude
        End Get
        Set
            m_Latitude = Value
        End Set
    End Property
    Private m_Latitude As Double

    Public Property Longitude() As Double
        Get
            Return m_Longitude
        End Get
        Set
            m_Longitude = Value
        End Set
    End Property
    Private m_Longitude As Double

    Public Property Term() As String
        Get
            Return m_Term
        End Get
        Set
            m_Term = Value
        End Set
    End Property
    Private m_Term As String

End Class

<Route("/lookup/searchterm", "GET")> _
Public Class SearchTermRequest
    Implements IReturn(Of SearchTermResponse)

    Public Property Term() As String
        Get
            Return m_Term
        End Get
        Set
            m_Term = Value
        End Set
    End Property
    Private m_Term As String

End Class

Public Class SearchTermResponse
    Implements IHasResponseStatus

    Public Property ResponseStatus() As ResponseStatus
        Get
            Return m_ResponseStatus
        End Get
        Set
            m_ResponseStatus = Value
        End Set
    End Property
    Private m_ResponseStatus As ResponseStatus

    Public Property Results() As List(Of SearchTerm)
        Get
            Return m_Results
        End Get
        Set
            m_Results = Value
        End Set
    End Property
    Private m_Results As List(Of SearchTerm)

End Class

<Route("/cached/lookup/searchterm")> _
Public Class CachedSearchTermRequest
    Implements IReturn(Of CachedSearchTermResponse)

    Public ReadOnly Property CacheKey() As String
        Get
            Return UrnId.Create(Of CachedSearchTermRequest)(String.Format("{0}", Me.Term))
        End Get
    End Property

    Public Property Term() As String
        Get
            Return m_Term
        End Get
        Set
            m_Term = Value
        End Set
    End Property
    Private m_Term As String

End Class

Public Class CachedSearchTermResponse
    Implements IHasResponseStatus

    Public Property ResponseStatus() As ResponseStatus
        Get
            Return m_ResponseStatus
        End Get
        Set
            m_ResponseStatus = Value
        End Set
    End Property
    Private m_ResponseStatus As ResponseStatus

    Public Property Results() As List(Of SearchTerm)
        Get
            Return m_Results
        End Get
        Set
            m_Results = Value
        End Set
    End Property
    Private m_Results As List(Of SearchTerm)

End Class

Public Class SearchTermService
    Inherits Service

    Public Property Repository() As IRepository
        Get
            Return m_Repository
        End Get
        Set
            m_Repository = Value
        End Set
    End Property
    Private m_Repository As IRepository

    Public Function [Get](request As SearchTermRequest) As SearchTermResponse
            Return New SearchTermResponse() With { _
                Key .Results = Me.Repository.SearchTermGet(request) _
            }
    End Function

End Class

Public Class CachedSearchTermService
    Inherits Service

    Public Property AppConfig() As AppConfig
        Get
            Return m_AppConfig
        End Get
        Set
            m_AppConfig = Value
        End Set
    End Property
    Private m_AppConfig As AppConfig

    Public Function [Get](request As CachedSearchTermRequest) As Object
        Dim cacheKey As String = request.CacheKey
        Return Me.RequestContext.ToOptimizedResultUsingCache(
            MyBase.Cache, cacheKey, New TimeSpan(0, Me.AppConfig.CacheTimeMinutes, 0),
                Function() 
                    Using service = Me.ResolveService(Of SearchTermService)()
                        Return service.[Get](request.TranslateTo(Of SearchTermRequest)()).TranslateTo(Of CachedSearchTermResponse)()
                    End Using
               End Function
            )
    End Function

End Class
于 2013-07-18T23:02:05.353 回答