longbi141 2019-09-27 21:46 采纳率: 0%
浏览 381

vb.net做redis接口 TCPclient连接发送接收数据如何扩大容量 stream的读写缓存如何扩大

如果调用get key或set key数据过大会报错,只能接收小数据的get和set
而redis存储字符串最大能有512M,这个接口我不知道怎么扩大数据发送和接收的上限

Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports vRedis.Commands

Public Class RedisClient
    Private client As TcpClient
    Private stream As NetworkStream
    Private reply As RedisReply
    Private command As IRedisCommand
    Private memStream As MemoryStream

    Public ReadOnly Property Host As String

    Public ReadOnly Property Port As Integer

    Public ReadOnly Property [Return] As Object
        Get
            Return reply.Value
        End Get
    End Property

    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)
            'TCPclient连接发送接收数据如何扩大容量
            stream = client.GetStream()
        Catch ex As Exception
            Throw New RedisException("An existing connection was forcibly closed by remote host.")
        End Try

    End Sub

    Public Sub Quit()
        command = New QuitCommand()
        Execute(command)
    End Sub

    Public Sub Append(key As String, value As String)
        command = New AppendCommand() With {.Key = key, .Value = value}
        Execute(command)
    End Sub

    Public Sub Del(ParamArray keys() As String)
        command = New DelCommand() With {.Keys = keys}
        Execute(command)
    End Sub

    Public Function Dump(key As String) As Byte()
        command = New DumpCommand() With {.Key = key}
        Execute(command)
        If IsNothing(reply.Value) Then
            Return Nothing
        Else
            Return Encoding.UTF8.GetBytes(reply.Value)
        End If
    End Function

    Public Sub Echo(message As String)
        command = New EchoCommand() With {.Message = message}
        Execute(command)
    End Sub

    Public Function Exists(key As String) As Boolean
        command = New ExistsCommand() With {.Key = key}
        Execute(command)
        Return reply.Value
    End Function

    Public Sub Expire(key As String, timeout As Integer)
        command = New ExpireCommand() With {.Key = key, .Timeout = timeout}
        Execute(command)
    End Sub

    Public Sub ExpireAt(key As String, ttl As DateTime)
        command = New ExpireAtCommand() With {.Key = key, .TTL = (ttl - #1/1/1970#).TotalSeconds}
        Execute(command)
    End Sub

    Public Function Get As String
        command = New GetCommand() With {.Key = key}
        Execute(command)
        Return reply.Value
    End Function

    Public Sub PExpire(key As String, timeout As Integer)
        command = New PExpireCommand() With {.Key = key, .Timeout = timeout}
        Execute(command)
    End Sub

    Public Sub PExpireAt(key As String, ttl As DateTime)
        command = New PExpireAtCommand() With {.Key = key, .TTL = (ttl - #1/1/1970#).TotalMilliseconds}
        Execute(command)
    End Sub

    Public Sub Ping()
        command = New PingCommand()
        Execute(command)
    End Sub

    Public Sub Auth(password As String)
        command = New AuthCommand() With {.Password = password}
        Execute(command)
    End Sub

    Public Sub Select
        command = New SelectCommand() With {.Index = index}
        Execute(command)
    End Sub

    Public Sub Set
        command = New SetCommand() With {.Key = key, .Value = value, .ExpireTime = expireTime, .Override = override}
        Execute(command)
    End Sub

    Public Function Time() As Date
        command = New TimeCommand()
        Execute(command)
        'Return #1/1/1970#.AddSeconds(reply.Value(0)).ToLocalTime()
    End Function

    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(65535)
            '这里stream的读写缓存如何扩大
            ReDim bytes(client.ReceiveBufferSize)
            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
End Class

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2019-09-28 10:27
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?