candy
godis is a realization of golang redis client reference jedis achieve.
godis achieve almost all of redis commands, including single command, the cluster command, the command pipeline and things commands.
If you have used jedis, you can easily get started godis, because godis method named almost entirely from jedis.
It is worth mentioning that, godis a distributed lock in stand-alone and cluster model, godis lock faster than redisson, in i7,8 nuclear 32g of computer testing, 10 million times for cycling, eight threads, the business logic is simple count ++, reidsson needs 18-20 seconds, and godis only about seven seconds.
godis has completed the test most commands, is relatively stable.
very glad that you asked any suggestions, I will actively iteration of this project.
Characteristic
- cluster Cluster
- pipeline pipeline
- transaction things
- distributed lock Distributed Lock
- Other features in the continuing development
installation
go get -u github.com/piaohao/godis
Or use go.mod
:
require github.com/piaohao/godis latest
File
Quick Start
-
Basic example
package main import ( "github.com/piaohao/godis" ) func main() { redis := godis.NewRedis(&godis.Option{ Host: "localhost", Port: 6379, Db: 0, }) defer redis.Close() redis.Set("godis", "1") arr, _ := redis.Get("godis") println(string(arr)) }
-
Use connection pooling
package main import ( "github.com/piaohao/godis" ) func main() { option:=&godis.Option{ Host: "localhost", Port: 6379, Db: 0, } pool := godis.NewPool(&godis.PoolConfig{}, option) redis, _ := pool.GetResource() defer redis.Close() redis.Set("godis", "1") arr, _ := redis.Get("godis") println(string(arr)) }
-
Publish and subscribe
package main import ( "github.com/piaohao/godis" "time" ) func main() { option:=&godis.Option{ Host: "localhost", Port: 6379, Db: 0, } pool := godis.NewPool(&godis.PoolConfig{}, option) go func() { redis, _ := pool.GetResource() defer redis.Close() pubsub := &godis.RedisPubSub{ OnMessage: func(channel, message string) { println(channel, message) }, OnSubscribe: func(channel string, subscribedChannels int) { println(channel, subscribedChannels) }, OnPong: func(channel string) { println("recieve pong") }, } redis.Subscribe(pubsub, "godis") }() time.Sleep(1 * time.Second) { redis, _ := pool.GetResource() defer redis.Close() redis.Publish("godis", "godis pubsub") redis.Close() } time.Sleep(1 * time.Second) }
-
cluster Cluster
package main import ( "github.com/piaohao/godis" "time" ) func main() { cluster := godis.NewRedisCluster(&godis.ClusterOption{ Nodes: []string{"localhost:7000", "localhost:7001", "localhost:7002", "localhost:7003", "localhost:7004", "localhost:7005"}, ConnectionTimeout: 0, SoTimeout: 0, MaxAttempts: 0, Password: "", PoolConfig: &godis.PoolConfig{}, }) cluster.Set("cluster", "godis cluster") reply, _ := cluster.Get("cluster") println(reply) }
-
pipeline pipeline
package main import ( "github.com/piaohao/godis" "time" ) func main() { option:=&godis.Option{ Host: "localhost", Port: 6379, Db: 0, } pool := godis.NewPool(&godis.PoolConfig{}, option) redis, _ := pool.GetResource() defer redis.Close() p := redis.Pipelined() infoResp, _ := p.Info() timeResp, _ := p.Time() p.Sync() timeList, _ := timeResp.Get() println(timeList) info, _ := infoResp.Get() println(info) }
-
transaction things
package main import ( "github.com/piaohao/godis" "time" ) func main() { option:=&godis.Option{ Host: "localhost", Port: 6379, Db: 0, } pool := godis.NewPool(nil, option) redis, _ := pool.GetResource() defer redis.Close() p, _ := redis.Multi() infoResp, _ := p.Info() timeResp, _ := p.Time() p.Exec() timeList, _ := timeResp.Get() println(timeList) info, _ := infoResp.Get() println(info) }
-
distribute lock Distributed Lock
-
single redis
package main import ( "github.com/piaohao/godis" "time" ) func main() { locker := godis.NewLocker(&godis.Option{ Host: "localhost", Port: 6379, Db: 0, }, &godis.LockOption{ Timeout: 5*time.Second, }) lock, err := locker.TryLock("lock") if err == nil && lock!=nil { //do something locker.UnLock(lock) } }
-
redis cluster
package main import ( "github.com/piaohao/godis" "time" ) func main() { locker := godis.NewClusterLocker(&godis.ClusterOption{ Nodes: []string{"localhost:7000", "localhost:7001", "localhost:7002", "localhost:7003", "localhost:7004", "localhost:7005"}, ConnectionTimeout: 0, SoTimeout: 0, MaxAttempts: 0, Password: "", PoolConfig: &godis.PoolConfig{}, },&godis.LockOption{ Timeout: 5*time.Second, }) lock, err := locker.TryLock("lock") if err == nil && lock!=nil { //do something locker.UnLock(lock) } }
-
certificate
godis
Using the MIT License , always 100% free and open source.
Acknowledgments
- jedis, java very famous redis client
- gf, very powerful go web framework
- go-commons-pool, with reference apache common-pool go to achieve connection pool