client = New TcpClient()
client.Connect(host, port)
stream = client.GetStream()
Dim bytes() As Byte = Encoding.UTF8.GetBytes(command.GetCommand() & vbCrLf)
stream.Write(bytes, 0, bytes.Length)
stream.Flush()
'ReDim bytes(Integer.MaxValue) 沒辦法設定byte的最大值,TcpClient傳輸限制了
'ReDim bytes(client.ReceiveBufferSize)
ReDim bytes(62420)
stream.Read(bytes, 0, bytes.Length)
Dim result = Encoding.UTF8.GetString(bytes)
做一個dll的redis介面
uj5u.com熱心網友回復:
可以換個協議玩玩。看見你的ID和結帖率,我也不想多說了uj5u.com熱心網友回復:
大哥用哪個協議,對這塊不懂
Public Sub New(Optional host As String = "127.0.0.1", Optional port As Integer = 6379)
If IsNothing(host) Then
Throw New ArgumentNullException(NameOf(host))
End If
Me.Host = host
Me.Port = port
client = New TcpClient()
Try
client.Connect(host, port)
stream = client.GetStream()
Catch ex As Exception
Throw New RedisException("An existing connection was forcibly closed by remote host.")
End Try
End Sub
Private Sub Execute(command As IRedisCommand)
Dim bytes() As Byte = Encoding.UTF8.GetBytes(command.GetCommand() & vbCrLf)
Try
stream.Write(bytes, 0, bytes.Length)
stream.Flush()
'ReDim bytes(Integer.MaxValue)
'ReDim bytes(client.ReceiveBufferSize)
ReDim bytes(62420)
stream.Read(bytes, 0, bytes.Length)
Dim result = Encoding.UTF8.GetString(bytes)
Select Case result(0)
Case "$"
Dim length = Convert.ToInt32(result.Substring(1, result.IndexOf(vbCrLf) - 1))
If length = -1 Then
reply = New RedisReply(RESPType.BulkString, Nothing)
Else
reply = New RedisReply(RESPType.BulkString, result.Substring(result.IndexOf(vbCrLf) + 2, length))
End If
Case "+"
reply = New RedisReply(RESPType.SimpleString, result.Substring(1, result.IndexOf(vbCrLf) - 1))
Case ":"
reply = New RedisReply(RESPType.Integer, Convert.ToInt32(result.Substring(1, result.IndexOf(vbCrLf) - 1)))
Case "-"
reply = New RedisReply(RESPType.Error, result.Substring(1, result.IndexOf(vbCrLf) - 1))
Throw New RedisException(reply.Value)
Case "*"
Dim count = Convert.ToInt32(result.Substring(1, result.IndexOf(vbCrLf) - 1))
Dim items = result.Split(New Char() {vbCrLf, vbLf}, StringSplitOptions.RemoveEmptyEntries).ToList()
items.RemoveAt(0)
items.RemoveAll(Function(i) i.StartsWith("$"))
items.RemoveAt(items.Count - 1)
reply = New RedisReply(RESPType.Array, items)
End Select
Catch ex As Exception
Throw New RedisException($"There is an internal error during executing '{command.GetCommand()}'.")
End Try
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/116357.html
標籤:VB.NET
上一篇:前端和后端對演算法要求都高嗎
