我正在评估一些选项,用一个可以在Web场共享的缓存系统替换System.Web.Caching.Cache。我目前正在评估redis,但速度非常慢。鉴于redis被广泛认可,我将redis实例托管在与调用它的Web服务器相同的服务器上,我认为我的实现中缺少一些东西。这是我的连接经理:
Public Class RedisConnection
Private Shared lazyConnection As New Lazy(Of ConnectionMultiplexer)(Function()
Return ConnectionMultiplexer.Connect(ConfigurationManager.ConnectionStrings("Redis").ConnectionString)
End Function)
Private Shared _database As IDatabase = lazyConnection.Value.GetDatabase()
Public Shared ReadOnly Property Connection() As ConnectionMultiplexer
Get
Return lazyConnection.Value
End Get
End Property
Public Shared ReadOnly Property Database() As IDatabase
Get
Return _database
End Get
End Property
Public Shared ReadOnly Property Server() As IServer
Get
Dim endpoints As EndPoint() = Connection.GetEndPoints(True)
Return Connection.GetServer(endpoints.First())
End Get
End Property
End Class
这是我的扩展:(我使用jil进行序列化ATM)
Public Module RedisExtensions
<Runtime.CompilerServices.Extension> _
Public Function [Get](Of T)(ByVal cache As IDatabase, ByVal key As String) As T
Return Deserialize(Of T)(cache.StringGet(key))
End Function
<Runtime.CompilerServices.Extension> _
Public Sub [Set](ByVal cache As IDatabase, ByVal key As String, ByVal value As Object)
cache.StringSet(key, Serialize(value))
End Sub
<Runtime.CompilerServices.Extension> _
Public Sub [Set](ByVal cache As IDatabase, ByVal key As String, ByVal value As Object, ByVal expiration As DateTime)
cache.StringSet(key, Serialize(value))
cache.KeyExpire(key, expiration)
End Sub
Private Function Deserialize(Of T)(ByVal value As Byte()) As T
If value Is Nothing Then
Return Nothing
End If
Return JSON.Deserialize(Of T)(Encoding.UTF8.GetString(value))
End Function
Private Function Serialize(ByVal value As Object) As Byte()
If value Is Nothing Then
Return Nothing
End If
Return Encoding.UTF8.GetBytes(JSON.Serialize(value))
End Function
End Module
任何突出显然都错了?引入比System.Web.Caching.Cache要慢得多的序列化/序列化和网络旅行?
谢谢!