search
HomeDatabaseRedisHow to connect golang redis client

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 
 <p>}<br></p><p>#The method encapsulates the three processes of a request: Send, Flush and Receive<br></p><p>1, send writes the request to the output buffer<br></p><p>2, Flush sends the buffer command to the server</p><p>3, Receive receives the response from the server</p><p>https://godoc.org/github.com/ gomodule/redigo/redis#hdr-Pipelining</p><p></p><pre class="brush:php;toolbar:false">// conn is the low-level implementation of Conn

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
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis vs databases: performance comparisonsRedis vs databases: performance comparisonsMay 14, 2025 am 12:11 AM

Redisoutperformstraditionaldatabasesinspeedforread/writeoperationsduetoitsin-memorynature,whiletraditionaldatabasesexcelincomplexqueriesanddataintegrity.1)Redisisidealforreal-timeanalyticsandcaching,offeringphenomenalperformance.2)Traditionaldatabase

When Should I Use Redis Instead of a Traditional Database?When Should I Use Redis Instead of a Traditional Database?May 13, 2025 pm 04:01 PM

UseRedisinsteadofatraditionaldatabasewhenyourapplicationrequiresspeedandreal-timedataprocessing,suchasforcaching,sessionmanagement,orreal-timeanalytics.Redisexcelsin:1)Caching,reducingloadonprimarydatabases;2)Sessionmanagement,simplifyingdatahandling

Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

See all articles

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools