访问频率越高,增长的越小,还会衰减(16位记载时间,8位记载逻辑访问次数)总结
附带的tcp毗连redis客户端 使用go编写的[code]package mainimport ( "bufio" "errors" "fmt" "io" "net" "strconv")// RedisClient 封装用于毗连type RedisClient struct { conn net.Conn //包装一层 方便读写 writer *bufio.Writer reader *bufio.Reader}func main() { //毗连redis conn, err := net.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("毗连redis失败", err) } client := RedisClient{conn: conn, writer: bufio.NewWriter(conn), reader: bufio.NewReader(conn)} defer client.conn.Close() //发送下令 client.sendRequest([]string{"set", "name", "方块"}) //zrange boards:2024-4 0 -1 //client.sendRequest([]string{"zrange", "boards:2024-4", "0", "-1"}) //LRANGE tlist 0 -1 client.sendRequest([]string{"LRANGE", "tlist", "0", "-1"})}func (client *RedisClient) sendRequest(args []string) interface{} { length := len(args) firstCommand := fmt.Sprintf("%s%d", "*", length) client.writeCommand(firstCommand) for _, s := range args { n := len(s) client.writeCommand("$" + strconv.Itoa(n)) client.writeCommand(s) println(n, s) } response := client.handleResponse() //fmt.Printf("%v", response) return response}// 写下令func (client *RedisClient) writeCommand(s string) { client.conn.Write([]byte(s + "\r\n"))}// 解析返回结果func (client *RedisClient) handleResponse() interface{} { //先读取第一个字符 r, _, _ := client.reader.ReadRune() flag := string(r) fmt.Println("第一个操作数:" + flag) switch flag { case "+": //一行字符串 return client.ReadLine() case "-": //异常 return client.ReadLine() case ":": //数字 line := client.ReadLine() res, _ := strconv.Atoi(line) return res case "$": // 多行字符串 //readRune, _, _ := client.reader.ReadRune() //去掉换行 readRune := client.ReadLine() length := string(readRune) if length == "-1" { return nil } else if length == "0" { return "" } lll, _ := strconv.Atoi(length) //+2是跳过\r\n bytes := make([]byte, lll+2) n, _ := client.reader.Read(bytes) return string(bytes[:n]) case "*": //多行字符串 递归获取 return client.readBulkString() default: return errors.New("错误") }}// 读一行func (client *RedisClient) ReadLine() string { bytes, _, _ := client.reader.ReadLine() return string(bytes)}// 读到末尾 估计有点标题func (client *RedisClient) ReadToEnd() string { var size = 1024 bytes := make([]byte, size) var temp = "" for { n, err := client.reader.Read(bytes) temp += string(bytes[:n]) //n, err := client.conn.Read(bytes) if err == io.EOF || n == 0 || n < size { break } } return temp}func (client *RedisClient) readBulkString() interface{} { counts, _ := strconv.Atoi(client.ReadLine()) if counts
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |