godis
redis client implement by golang, inspired by jedis
About godis
godis
redis client implement by golang, refers to jedis.
this library implements most of redis command, include normal redis command, cluster command, sentinel command, pipeline command and transaction command.
if you've ever used jedis, then you can use godis easily, godis almost has the same method of jedis.
especially, godis implements distributed lock in single mode and cluster mode, godis's lock is much more faster than redisson, on my computer(i7,8core,32g), run 100,000 loop, use 8 threads, the business code is just count++, redisson need 18s-20s, while godis just need 7 second.
godis has done many test case to make sure it's stable.
I am glad you made any suggestions, and I will actively iterate over the project.
Features
- cluster
- pipeline
- transaction
- distributed lock
- other feature under development
Installation
go get -u github.com/piaohao/godis
or use go.mod:
require github.com/piaohao/godis latest
Documentation
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(arr) } -
use pool
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(arr) } -
pubsub
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
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
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
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
- 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) } }
- single redis
License
godis is licensed under the MIT License, 100% free and open-source, forever.
Thanks
- [ jedis ] jedis is a popular redis client write by java
- [ gf ] gf is a amazing web framework write by golang
- [ go-commons-pool ] refers to apache comnmon-pool
Contact
Frequently Asked Questions
What is godis?
godis is a Database Drivers library for the Go programming language. redis client implement by golang, inspired by jedis
How do I install godis?
Install godis with the Go module system using `go get piaohao/godis`. Check the repository for the current installation instructions.
What category does godis belong to?
godis is listed under Database Drivers, specifically NoSQL Database Drivers.