go語言redis-cluster開源用戶端

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。傳送門 https://github.com/gitstliu/go-redis-cluster### If you like, Please Start it# go-redis-clustergo-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## InstallationInstall go-redis-cluster with go tool:``` go get github.com/gitstliu/go-redis-cluster``` ## UsageTo use redis cluster, you need import the package and create a new cluster clientwith an 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 command, it need a key to hash to a slot, then find the corresponding redis node. Do method will choose firstargument 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```gocluster.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.```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 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 stringvar val2 intreply, err = Scan(reply, &val1, &val2)var val3 float64reply, err = Scan(reply, &val3)var val4 boolreply, err = Scan(reply, &val4)```### Multi-keysMutiple 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.### PipeliningPipelining 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 distinctnodes 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.```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 have any question, please email me gitstliu@163.com.## Licensego-redis-cluster is available under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html).421 次點擊  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.