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 type2. 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 ":" colonFormat:: Number \r\n eg: ":1000\r\n"4. Large string type Bulk Strings, starting with the "$" dollar sign , length limit 512MFormat: $ 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" null5. Array type Arrays, starting with "*" asteriskFormat: *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!

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

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

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 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.

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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
Integrate Eclipse with SAP NetWeaver application server.

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
Visual web development tools
