代码拉取完成,页面将自动刷新
package RedisClient
import (
"bufio"
"errors"
"fmt"
"net"
"strconv"
)
const (
CRLF = "\r\n"
redisArray = '*'
redisString = '$'
redisInt = ':'
redisSampleString = '+'
redisSampleErrorString = '-'
)
type RedisInfo struct {
address string
Conn net.Conn
reader *bufio.Reader
bufRead []string
}
// 创建连接
func Client(address string,password string,db int) (client RedisInfo, err error) {
fmt.Printf("连接Redis Server... , address: %s , password: %s , port: %d\n",address,password,db)
client.address = address
client.Conn , err = net.Dial("tcp",client.address)
client.reader = bufio.NewReader(client.Conn)
// redis设置
if "" != password {
_,err = client.auth(password)
}
if 0 != db {
_,err = client.Run("SELECT",strconv.Itoa(db))
}
return
}
// 发送数据
func (client *RedisInfo)connWrite(buf []byte) (error) {
writeLen := 0
for writeLen < len(buf) {
n , err := client.Conn.Write(buf[writeLen:])
if err != nil {
return err
}
writeLen += n
}
return nil
}
// 接收数据
func (client *RedisInfo)respRead()(buf []string,err error) {
newLine, _, _ := client.reader.ReadLine()
switch newLine[0] {
case redisArray:
count, err := byteToInt(newLine[1:])
fmt.Println("count:",count)
if err != nil {
return nil, err
}
return client.readArrayResp(count)
case redisSampleString:
buf = append(buf, string(newLine[1:]))
case redisInt:
buf = append(buf, string(newLine[1:]))
case redisSampleErrorString:
err = errors.New(string(newLine[1:]))
case redisString:
n, _ := byteToInt(newLine[1:])
newBuf := make([]byte, n)
client.reader.Read(newBuf)
buf = append(buf, string(newBuf))
crlf := make([]byte, 2)
client.reader.Read(crlf)
}
return
}
// 获取resp数组
func (client *RedisInfo)readArrayResp(count int) (buf []string , err error) {
for i:=0;i<count;i++ {
newBuf , _ := client.readSampleResp()
buf = append(buf,newBuf...)
}
return
}
// 获取其他数据类型
func (client *RedisInfo)readSampleResp()(buf []string , err error) {
newLine , _ , _ := client.reader.ReadLine()
switch newLine[0] {
case redisInt:
buf = append(buf,string(newLine[1:]))
case redisString:
n,_ := byteToInt(newLine[1:])
newBuf := make([]byte,n)
client.reader.Read(newBuf)
buf = append(buf,string(newBuf))
crlf := make([]byte,2)
client.reader.Read(crlf)
}
return
}
// 字符转换为数字
func byteToInt(number []byte) (int , error) {
var sum int
x := 1
for i:=len(number)-1;i>=0;i-- {
if number[i] < '0' || number[i] > '9' {
return 0 , errors.New("toNumberError")
}
sum += int(number[i] - '0') * x
x = x * 10
}
return sum,nil
}
// run
func (client *RedisInfo)Run(com ...string)([]string,error) {
buf , _:= toRESP(com)
client.connWrite(buf)
return client.respRead()
}
// auth
func (client *RedisInfo)auth(password string)([]string,error) {
var com []string
com = append(com,"AUTH")
com = append(com, password)
buf , _ := toRESP(com)
client.connWrite(buf)
return client.respRead()
}
// Set
func (client *RedisInfo)Set(key string,value string,seconds string,milliseconds string) ([]string,error) {
fmt.Printf("set操作 , key: %s , val: %s , seconds: %s , milliseconds: %s \n",key,value,seconds,milliseconds)
var com []string
com = append(com,"SET")
com = append(com, key)
com = append(com,value)
if seconds != "" {
com = append(com,"EX")
com = append(com,seconds)
}
if milliseconds != "" {
com = append(com,"PX")
com = append(com,milliseconds)
}
buf , _ := toRESP(com)
fmt.Println("set待发送的报文: " , string(buf))
client.connWrite(buf)
return client.respRead()
}
// HSet
func (client *RedisInfo)HSet(key string,field string,value string) ([]string,error) {
var com []string
com = append(com,"HSET")
com = append(com, key)
com = append(com, field)
com = append(com, value)
buf , _ := toRESP(com)
client.connWrite(buf)
return client.respRead()
}
// HGelAll
func (client *RedisInfo)HGelAll(key string) ([]string,error) {
var com []string
com = append(com,"HGETALL")
com = append(com, key)
buf , _ := toRESP(com)
client.connWrite(buf)
return client.respRead()
}
// HGet
func (client *RedisInfo)HGet(key string,field string) ([]string,error) {
var com []string
com = append(com,"HGET")
com = append(com, key)
com = append(com,field)
buf , _ := toRESP(com)
client.connWrite(buf)
return client.respRead()
}
// Get
func (client *RedisInfo)Get(key string) ([]string,error) {
fmt.Printf("get操作 , key: %s\n",key)
var com []string
com = append(com, "GET")
com = append(com,key)
buf , _ := toRESP(com)
fmt.Println("Get待发送报文: " , string(buf))
client.connWrite(buf)
return client.respRead()
}
// 将命令转化为RESP格式的报文
func toRESP(command []string) (resp []byte,err error) {
arrayLen := len(command)
if 0 == arrayLen {
// 命令为空
return
}
// 将长度写入到resp中
resp = append(resp, redisArray)
resp = append(resp, []byte(strconv.Itoa(arrayLen))...)
resp = append(resp,[]byte(CRLF)...)
// 开始遍历数组
for _,v := range command {
// 获取数据长度
vLen := len(v)
resp = append(resp,redisString)
resp = append(resp, []byte(strconv.Itoa(vLen))...)
resp = append(resp,[]byte(CRLF)...)
resp = append(resp,[]byte(v)...)
resp = append(resp,[]byte(CRLF)...)
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。