Go Language Redis-cluster Open source client

Source: Internet
Author: User
Tags redis cluster
This is a creation in Article, where the information may have evolved or changed. Portal https://github.com/gitstliu/go-redis-cluster### If You like, please Start it# Go-redis-clustergo-redis-cluster is a G Olang implementation of Redis client based on Gary Burd ' S[redigo] (Https://github.com/garyburd/redigo). It caches slot info at the local and updates it automatically when the cluster change. The client manages a connection pool for each node, uses Goroutine to execute as concurrently as possible, which leads to Its high efficiency and low lantency.**supported**:* Most commands of keys, strings, lists, sets, sorted sets, hashes.* MG et/mset* pipelining**not supported**:* Cluster commands* pub/sub* transaction* Lua script## installationinstall Go-redis-cluster with Go tool: ' Go get github.com/gitstliu/go-redis-cluster ' # # # Usageto Use Redis cluster, you need IM Port the package and create a new cluster clientwith a options: ' ' Goimport ' github.com/gitstliu/go-redis-cluster ' cluster , err: = Redis. Newcluster (&redis. Options{startnodes: []string{] 127.0.0.1:7000 "," 127.0.0.1:7001 "," 127.0.0.1:7002 "},conntimeout:50 * time. MILLISECOND,READTIMEOUT:50 * time. MILLISECOND,WRITETIMEOUT:50 * time. Millisecond,keepalive:16,alivetime:60 * time. Second,}) "' # # # Basicgo-redis-cluster has compatible interface to [Redigo] (Https://github.com/garyburd/redigo), which Uses a print-like API for all Redis commands. When executing a is command, it need a key to hash to a slot, then find the corresponding Redis node. Do method would choose Firstargument in args as the key, so commands which is independent from keys is not Supported,such As SYNC, BGSAVE, Randomkey, etc **restriction**: Please be sure the first argument on args is key. See full Redis commands:http://www.redis.io/commands ' Gocluster. Do ("SET", "foo", "Bar") cluster. Do ("INCR", "Mycount", 1) cluster. Do ("Lpush", "MyList", "foo", "Bar") cluster. Do ("Hmset", "Myhash", "F1", "foo", "F2", "bar") ' can use the Help functions to convert reply to int, float, string, etc. ' ' Goreply, err: = Int (cluster. DO ("INCR", "Mycount", 1)) reply, Err: = String (cluster. Do ("GET", "foo")) reply, Err: = Strings (cluster. Do ("Lrange", "MyList", 0,-1)) reply, Err: = Stringmap (cluster. Do ("Hgetall", "Myhash")) ' Also, you can use the values and Scan to convert replies to multiple values with different types. ' ' Go_, err: = cluster. Do ("MSET", "Key1", "foo", "Key2", 1024x768, "Key3", 3.14, "Key4", "false") reply, err: = Values (cluster. Do ("MGET", "Key1", "Key2", "Key3", "Key4")) var val1 stringvar val2 intreply, err = Scan (reply, &val1, &val2) var va L3 float64reply, err = scan (reply, &val3) var val4 boolreply, err = scan (reply, &val4) ' # # # Multi-keysmutiple Keys Command-mget/mset is supported using result aggregation. Processing steps is as follows:-first, split the keys into multiple nodes according to their hash slot.-then, start a G Oroutine for each node to Excute mget/mset commands and wait them finish.-last, collect and rerange all replies, return B Ack to caller.**note**: Since the keys May SpreaD across Mutiple node, there's no atomicity gurantee that all keys would be set at once. It's possible that some keys be set while others be not.### pipeliningpipelining is supported through the Batch INTERFAC E. You can put multiple commands to a batch as long as it is supported by do method. Runbatch would split these command to Distinctnodes and start a goroutine for each node. Commands hash to same nodes would be merged and sent using pipelining. After all commands did, it rearrange results as mget/mset do. Result is a slice of each command ' s reply, you can use the Scan to convert them to other types. "Gobatch: = cluster. Newbatch () Err = batch. Put ("Lpush", "Country_list", "France") Err = batch. Put ("Lpush", "Country_list", "Italy") Err = batch. Put ("Lpush", "Country_list", "Germany") Err = batch. Put ("Incrby", "Countries", 3) Err = batch. Put ("Lrange", "Country_list", 0,-1) reply, err = cluster. Runbatch (Batch) var resp intfor I: = 0; I < 4; i++ {reply, err = Redis. Scan (reply, &resp)}cOuntries, err: = Strings (reply[0], nil) ' ' # # CONTACTBUG reports and feature requests are welcome. If you had any question, please email me gitstliu@163.com.## licensego-redis-cluster is available under the [Apache Licen SE, Version 2.0] (http://www.apache.org/licenses/LICENSE-2.0.html). 421 reads  
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.