gitstliu · · 3621 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

传送门 https://github.com/gitstliu/go-redis-cluster ### If you like, Please Start it # go-redis-cluster go-redis-cluster is a golang implementation of redis client based on Gary Burd's [Redigo](https://github.com/garyburd/redigo). It caches slot info at local and updates it automatically when 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. * MGET/MSET * Pipelining **NOT supported**: * Cluster commands * Pub/Sub * Transaction * Lua script ## Installation Install go-redis-cluster with go tool: ``` go get github.com/gitstliu/go-redis-cluster ``` ## Usage To use redis cluster, you need import the package and create a new cluster client with an options: ```go import "github.com/gitstliu/go-redis-cluster" cluster, err := redis.NewCluster( &redis.Options{ StartNodes: []string{"", "", ""}, ConnTimeout: 50 * time.Millisecond, ReadTimeout: 50 * time.Millisecond, WriteTimeout: 50 * time.Millisecond, KeepAlive: 16, AliveTime: 60 * time.Second, }) ``` ### Basic go-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 command, it need a key to hash to a slot, then find the corresponding redis node. Do method will choose first argument in args as the key, so commands which are independent from keys are not supported, such as SYNC, BGSAVE, RANDOMKEY, etc. **RESTRICTION**: Please be sure the first argument in args is key. See full redis commands: http://www.redis.io/commands ```go cluster.Do("SET", "foo", "bar") cluster.Do("INCR", "mycount", 1) cluster.Do("LPUSH", "mylist", "foo", "bar") cluster.Do("HMSET", "myhash", "f1", "foo", "f2", "bar") ``` You can use help functions to convert reply to int, float, string, etc. ```go reply, 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 Values and Scan to convert replies to multiple values with different types. ```go _, err := cluster.Do("MSET", "key1", "foo", "key2", 1024, "key3", 3.14, "key4", "false") reply, err := Values(cluster.Do("MGET", "key1", "key2", "key3", "key4")) var val1 string var val2 int reply, err = Scan(reply, &val1, &val2) var val3 float64 reply, err = Scan(reply, &val3) var val4 bool reply, err = Scan(reply, &val4) ``` ### Multi-keys Mutiple keys command - MGET/MSET are supported using result aggregation. Processing steps are as follows: - First, split the keys into multiple nodes according to their hash slot. - Then, start a goroutine for each node to excute MGET/MSET commands and wait them finish. - Last, collect and rerange all replies, return back to caller. **NOTE**: Since the keys may spread across mutiple node, there's no atomicity gurantee that all keys will be set at once. It's possible that some keys are set while others are not. ### Pipelining Pipelining is supported through the Batch interface. You can put multiple commands into a batch as long as it is supported by Do method. RunBatch will split these command to distinct nodes and start a goroutine for each node. Commands hash to same nodes will be merged and sent using pipelining. After all commands done, it rearrange results as MGET/MSET do. Result is a slice of each command's reply, you can use Scan to convert them to other types. ```go batch := 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 int for i := 0; i < 4; i++ { reply, err = redis.Scan(reply, &resp) } countries, err := Strings(reply[0], nil) ``` ## Contact Bug reports and feature requests are welcome. If you have any question, please email me gitstliu@163.com. ## License go-redis-cluster is available under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html).


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3621 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传