Table of Contents
The format (type) of the command sent by the client: 5 types
Home Database Redis How to connect golang redis client

How to connect golang redis client

May 29, 2023 am 09:14 AM
redis golang

Implemented the redis client, including a connection pool and redis pipleline

conn.go

func (c *conn) Do(cmd string, args ...interface {}) (interface{}, error){

if cmd != "" {    if err := c.writeCommand(cmd, args); err != nil {      return nil, c.fatal(err)    }  }    if err := c.bw.Flush(); err != nil {    return nil, c.fatal(err)  }      for i := 0; i <= pending; i++ {    var e error    if reply, e = c.readReply(); e != nil {      return nil, c.fatal(e)    }    if e, ok := reply.(Error); ok && err == nil {      err = e    }  }
Copy after login


#The method encapsulates the three processes of a request: Send, Flush and Receive

1, send writes the request to the output buffer

2, Flush sends the buffer command to the server

3, Receive receives the response from the server

https://godoc.org/github.com/ gomodule/redigo/redis#hdr-Pipelining

// conn is the low-level implementation of Conn
Copy after login

Because redis is a text protocol, it needs to be serialized according to the redis protocol when sending, and deserialized according to the redis protocol when receiving.

The format (type) of the command sent by the client: 5 types

The interval symbol is \r\n under Linux and \n

# under Windows. ##1. Simple Strings, starting with " "plus sign

Format: String\r\n

String cannot contain CR or LF (line breaks are not allowed)

eg: " OK\r\n"

Note: In order to send binary-safe strings, it is generally recommended to use the following Bulk Strings type

2. Errors, with " -"Begins with a minus sign

Format: - Error prefix Error message \r\n

Error message cannot contain CR or LF (line breaks are not allowed), Errors are very similar to Simple Strings, different Erros will be treated as an exception

eg: "-Error unknow command 'foobar'\r\n"

3. Integer type Integer, starting with ":" colon

Format:: Number \r\n

eg: ":1000\r\n"

4. Large string type Bulk Strings, starting with the "$" dollar sign , length limit 512M

Format: $ String length \r\n String \r\n

String cannot contain CR or LF (line breaks are not allowed);

    eg: "$

6\r\nfoobar\r\n"  where the string is foobar, and 6 is the character length of foobar

                  "$0 \r\n\r\n" " Empty string

"$-1\r\n" "$-1\r\n" null

5. Array type Arrays, starting with "*" asterisk

Format: *Number of array elements \r\n All other types (no need for \r\n at the end)

Note: Only the \r\n after the number of elements belongs to the array, The \r\n at the end is usually the

of the element eg: "*0\r\n" Empty array

"*2\r\n$2\r\nfoo\r\n $3\r\nbar\r\n" The array contains 2 elements, which are the string foo and bar

  "*3\r\n:1\r\n:2\r\n:3 \r\n" " The array contains 3 integers: 1, 2, 3

"*5\r\n:1\r\n:2\r\n:3\r\n:4\ r\n$6\r\nfoobar\r\n" Array containing mixed types

"*-1\r\n" "Null array

"*2\r\n*3 \r\n:1\r\n:2\r\n:3\r\n*2\r\n Foo\r\n-Bar\r\n" Nested arrays, the outer array contains 2 Arrays, after sorting as follows:

"*2\r\n

   *3\r\n:1\r\n:2\r\n:3\r\n

   *2\r\n Foo\r\n-Bar\r\n"

The above is the detailed content of How to connect golang redis client. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang and C  : Concurrency vs. Raw Speed Golang and C : Concurrency vs. Raw Speed Apr 21, 2025 am 12:16 AM

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

How to configure slow query log in centos redis How to configure slow query log in centos redis Apr 14, 2025 pm 04:54 PM

Enable Redis slow query logs on CentOS system to improve performance diagnostic efficiency. The following steps will guide you through the configuration: Step 1: Locate and edit the Redis configuration file First, find the Redis configuration file, usually located in /etc/redis/redis.conf. Open the configuration file with the following command: sudovi/etc/redis/redis.conf Step 2: Adjust the slow query log parameters in the configuration file, find and modify the following parameters: #slow query threshold (ms)slowlog-log-slower-than10000#Maximum number of entries for slow query log slowlog-max-len

Golang's Impact: Speed, Efficiency, and Simplicity Golang's Impact: Speed, Efficiency, and Simplicity Apr 14, 2025 am 12:11 AM

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Golang vs. C  : Performance and Speed Comparison Golang vs. C : Performance and Speed Comparison Apr 21, 2025 am 12:13 AM

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

See all articles