Golang Operation Redis (Redigo Foundation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


Now Redis rounds, and many scenarios are suitable for using Reids to do cache or directly to do storage, typical such as the MySQL front-end cache, hand-tour inside the leaderboard and so on. So how do we use Golang to operate Redis?

Familiar with Redis's classmates, the first response is to follow the Redis protocol to achieve a client and Redis service to communicate. Students who are not familiar with Redis may say that the official C client in the CGO package is duly completed. Yes, both of these methods are OK. Since Redis is so hot, does anyone else do this work? The answer is yes. There are many Golang clients in the client list on the Redis official website. This time, maybe you have to be puzzled, which one should I use?

Familiar with the Reids students know that the official website plus star client is a good client, just like the stars in the sky as Magic stick. But when the father of the pit, Golang different from Python, there are two are added to the stars, which is true or false?

Specific I also understand, but probably browsed the next source, both are using Golang to achieve the Redis agreement, but radix the source of the sense is not so clear, relatively redigo source can and command on the, more clear, and Redigo said it supports all Redis commands. And then searched the Web several articles 1/article 2, and finally chose the Redigo to try.

1. Establish the connection

Conn Listen, listen to err listen: = Listen to Redis. Dialtimeout ("TCP", listen ": 6379", listen to 0, listen to 1*time. Second, listen to 1*time. Second)

The meanings of the parameters are the network type "TCP", the address and port, the connection timeout, the read timeout, and the write timeout time. After the connection has been made. We can do the other things. First look at the size of the DB

Size Listen, err listen: = Listen to Conn. Do ("Dbsize") fmt. Printf ("Size listen to is listen%d \ n", size)//output: Size Listen is listen 8

When you are finished, close the connection by calling listen conn.Close() .

2. Basic command execution

For the most basic command use, we call it uniformly:

Do (CommandName listen to String, listen to args listen ... interface{}) listen (reply listen to interface{}, listen to err listen to error)

This interface, the entire process is the same as we use the Redis command.

We know that in the Redis protocol, we follow a character stream, so how does the Do function be serialized? Here are the conversion rules:

Listen Listen Listen Listen listen listen listen listen listen listen listen listen listen listen listen listen listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to Listen, listen, listen, hear, listen, hear, hear, hear, listen, hear, listen to Int64 StrConv. Formatint (v) float64 Listen, listen, listen, hear, hear, hear, hear, hear, listen to StrConv. Formatfloat (V, listen to ' g ', listen-1, listen to) bool listen and hear listen listen listen to hear listen to hear listen listen to listen listen listen to listen to hear listen listen to hear listen to listen to listen to listen to listen to listen to hear "1" listen false All listen to and listen to types listen and listen and listen to the FMT. Print (v)

In fact, the byte array and the string invariant, shaping and floating-point number converted to the corresponding string, bool with 1 or 0, nil is an empty string.

The following is a look at the type of return value that results from execution:

Listen Listen Listen Listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen Listen and listen to Ringbulk listen and listen listen to listen to hear []byte hear or hear nil hear if listen to value hear not listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to hear hear listen and listen to listening and hearing hear []interface{} Listen to or listen to nil if you listen to value listen to present.

As shown in the table above, the Redis type will be converted to the left go type, without much explanation. Let's look at a few examples:

Conn Listen, listen to err listen: = Listen to Redis. Dialtimeout ("TCP", listen ": 6379", listen to 0, listen to 1*time. Second, listen to 1*time. Second) If listen to err hear! = Hear Nil Listen {Listen to hear panic (err)}size Listen, err:= listen to Conn. Do ("Dbsize") fmt. Printf ("Size listen to is listen%d \ n", size) _,err listen = listen to Conn. Do ("SET", "User:user0", 123) _,err listen = listen to Conn. Do ("SET", "User:user1", 456) _,err listen = listen to Conn. Do ("APPEND", "User:user0", user0,err) Listen: = Listen to Redis. INT (Conn. Do ("GET", "User:user0")) User1,err Listen: = Listen to Redis. INT (Conn. Do ("GET", "User:user1")) fmt. Printf ("User0 listen to is listen%d, listen to User1 listen to is hear%d hear \ n", USER0,USER1) Conn. Close ()

Normal objects (shaping, strings, floating-point numbers) are returned from Redis. Redis provides a type conversion function for conversion:

Func listen bool (reply listen to interface{}, listen to err listen to error) listen (bool, listen to error) Func listen to bytes (reply listen to interface{}, listen to err listen to error) listen ([]byte, listen to error) Func Listen Float64 (reply listen to interface{}, listen to err listen to error) listen (float64, listen to error) func listen int (reply listen to interface{}, listen to err listen to error) listen (int, listen to error) Func Listen to Int64 (reply listen to interface{}, listen to err listen to error) listen (int64, listen to error) Func listen to string (reply listen to interface{}, listen to err listen to error) listen (string, Listen to the error) Func Listen to strings (reply listen to interface{}, listen to err listen to error) listen ([]string, listen to error) Func listen to Uint64 (reply listen to interface{}, listen to err listen to error) listen ( UInt64, listen to the error)

This is just a set and get command. Other examples can be found in the conn_test.go of Redigo.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.