In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of Go operation of redis and redigo, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Go operation redis and redigo method article. Let's take a look at it.
Go- operation redis installation
There are several client packages for golang to operate redis, such as redigo, and redigo has the most Star on go-redis,github.
Go get github.com/garyburd/redigo/redisimport "github.com/garyburd/redigo/redis" connection
The Conn interface is the main interface for collaboration with Redis. You can use the Dial,DialWithTimeout or NewConn function to create a connection. When the task is completed, the application must call the Close function to complete the operation.
Package mainimport ("github.com/garyburd/redigo/redis"fmt") func main () {conn,err: = redis.Dial ("tcp", "10.1.210.69 fmt") if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close ()} use package mainimport ("github.com/garyburd/redigo/redis"fmt") func main () {conn Err: = redis.Dial ("tcp", "10.1.210.69ore6379") if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () _, err = conn.Do ("SET", "name", "wd") if err! = nil {fmt.Println ("redis set error:", err)} name Err: = redis.String (conn.Do ("GET", "name")) if err! = nil {fmt.Println ("redis get error:", err)} else {fmt.Printf ("Got name:% s\ n", name)}} set key expiration time _, err = conn.Do ("expire", "name") 10) / / 10 seconds expired if err! = nil {fmt.Println ("set expire error:", err) return} batch mget, batch setting mset_, err = conn.Do ("MSET", "name", "wd", "age", 22) if err! = nil {fmt.Println ("redis mset error:", err)} res, err: = redis.Strings (conn.Do ("MGET") "name", "age") if err! = nil {fmt.Println ("redis get error:", err)} else {res_type: = reflect.TypeOf (res) fmt.Printf ("res type:% s\ n", res_type) fmt.Printf ("MGET name:% s\ n" Res) fmt.Println (len (res))} / / result: / / res type: [] string / / MGET name: [wd 22] / / 2 list operation package mainimport ("github.com/garyburd/redigo/redis"fmt"reflect") func main () {conn,err: = redis.Dial ("tcp", "10.1.210.69len 6379") if err! = nil {fmt.Println ("connect redis error:" Err) return} defer conn.Close () _, err = conn.Do ("LPUSH", "list1", "ele1", "ele2", "ele3") if err! = nil {fmt.Println ("redis mset error:", err)} res, err: = redis.String (conn.Do ("LPOP", "list1") if err! = nil {fmt.Println ("redis POP error:" Err)} else {res_type: = reflect.TypeOf (res) fmt.Printf ("res type:% s\ n", res_type) fmt.Printf ("res:% s\ n", res)}} / / res type: string / / res: ele3hash Operation package mainimport ("github.com/garyburd/redigo/redis"fmt"reflect") func main () {conn,err: = redis.Dial ("tcp") "10.1.210.69connect redis error 6379") if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () _, err = conn.Do ("HSET", "student", "name", "wd", "age", 22) if err! = nil {fmt.Println ("redis mset error:", err)} res Err: = redis.Int64 (conn.Do ("HGET", "student", "age")) if err! = nil {fmt.Println ("redis HGET error:", err)} else {res_type: = reflect.TypeOf (res) fmt.Printf ("res type:% s\ n", res_type) fmt.Printf ("res:% d\ n" Res)}} / / res type: int64 / / res: 22Pipelining (pipeline)
Pipeline operations can be understood as concurrent operations and are implemented through three methods: Send (), Flush () and Receive (). The client can use the send () method to send one or more commands to the server at one time. When the command is sent, the flush () method is used to send the command input of the buffer to the server at one time, and the client uses the Receive () method to read all the command operation results in the first-in-first-out order.
Send (commandName string, args... interface {}) errorFlush () errorReceive () (reply interface {}, err error)
Send: sending commands to buffer
Flush: clear the buffer and send the command to the server at once
Recevie: read the server response results in turn, and when the read command does not respond, the operation will block.
Package mainimport ("github.com/garyburd/redigo/redis"fmt") func main () {conn,err: = redis.Dial ("tcp", "10.1.210.69 conn,err") if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () conn.Send ("HSET", "student", "name", "wd", "age" "22") conn.Send ("HSET", "student", "Score", "100") conn.Send (" HGET "," student "," age ") conn.Flush () res1, err: = conn.Receive () fmt.Printf (" Receive res1:%v\ n ", res1) res2, err: = conn.Receive () fmt.Printf (" Receive res2:%v\ n ", res2) res3 Err: = conn.Receive () fmt.Printf ("Receive res3:%s\ n", res3)} / / Receive res1:0 / / Receive res2:0//Receive res3:22redis launch subscription model package mainimport ("github.com/garyburd/redigo/redis"fmt"time") func Subs () {/ / subscriber conn, err: = redis.Dial ("tcp") "10.1.210.69 connect redis error 6379") if err! = nil {fmt.Println ("connect redis error:" Err) return} defer conn.Close () psc: = redis.PubSubConn {conn} psc.Subscribe ("channel1") / / subscribe to channel1 channel for {switch v: = psc.Receive (). (type) {case redis.Message: fmt.Printf ("% s: message:% s\ n", v.Channel V.Data) case redis.Subscription: fmt.Printf ("% s:% s% d\ n", v.Channel, v.Kind, v.Count) case error: fmt.Println (v) return}} func Push (message string) {/ / publisher conn, _: = redis.Dial ("tcp", "10.1.210.69v.Channel 6379") _ Err1: = conn.Do ("PUBLISH", "channel1", message) if err1! = nil {fmt.Println ("pub err:", err1) return}} func main () {go Subs () go Push ("this is wd") time.Sleep (time.Second*3)} / / channel1: subscribe 1//channel1: message: this is wd transaction operation
MULTI, EXEC,DISCARD, and WATCH are the basis of Redis transactions, and of course we essentially use these commands when we use the go language to transact on redis.
MULTI: start a transaction
EXEC: performing transactions
DISCARD: canceling transaction
WATCH: monitors for key changes in a transaction and cancels the transaction if there is a change.
Example:
Package mainimport ("github.com/garyburd/redigo/redis"fmt") func main () {conn,err: = redis.Dial ("tcp", "10.1.210.69 conn,err") if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () conn.Send ("MULTI") conn.Send ("INCR", "foo") conn.Send ("INCR") "bar") r, err: = conn.Do ("EXEC") fmt.Println (r)} / / [1,1] Universal Operation connection redisconn,err: = redis.Dial ("tcp", "10.0.3.100 fmt.Println 6379", redis.DialPassword ("EfcHGSzKqg6cfzWq"), redis.DialDatabase (8) if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () write / / write / _ Err = conn.Do ("LPUSH", "list1", "ele1", "ele2", "ele3") _, err = conn.Do ("reids write method", "key name", "content 1", "content 2", "content 3") if err! = nil {fmt.Println ("redis set error:" Err)} read / / read redis.Strings: return multiple redis.String: return a redis.int: return statistics / / get all members of the collection / / name, err: = redis.Strings (conn.Do ("smembers", "beautiful_user")) / / return the number of members of the collection / / name, err: = redis.Int (conn.Do ("scard", "beautiful_user")) name, err: = redis. Method name (conn.Do ("redis read method", "key name") if err! = nil {fmt.Println ("redis get error:", err)} else {fmt.Printf ("Got name:% s\ n", name)} all code package mainimport ("fmt"github.com/garyburd/redigo/redis") func main () {conn,err: = redis.Dial ("tcp", "10.0.3.100mov6379" Redis.DialPassword ("EfcHGSzKqg6cfzWq"), redis.DialDatabase (8) if err! = nil {fmt.Println ("connect redis error:", err) return} defer conn.Close () / / write _, err = conn.Do ("LPUSH", "list1", "ele1", "ele2" "ele3") if err! = nil {fmt.Println ("redis set error:", err)} / / read name, err: = redis.Strings (conn.Do ("smembers", "beautiful_user")) if err! = nil {fmt.Println ("redis get error:" Err)} else {fmt.Printf ("Got name:% s\ n", name)}} on "how Go operates redis and redigo" ends here. Thank you for reading! I believe you all have a certain understanding of "the method of Go operating redis and redigo". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.