文章首发于:clawhub.club
项目中用到了redis中间件,所以找了个go版的redis客户端,简单封装后使用。
使用的go组件的github地址:
https://github.com/go-redis/redis
简单使用
redis_cmd.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package redis
import ( "github.com/go-redis/redis" "time" )
func Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd { if cfg.Cluster { return redisClusterClient.Set(key, value, expiration) } else { return client.Set(key, value, expiration) } }
func Get(key string) *redis.StringCmd { if cfg.Cluster { return redisClusterClient.Get(key) } else { return client.Get(key) } }
func Subscribe(channels ...string) *redis.PubSub { if cfg.Cluster { return redisClusterClient.Subscribe(channels...) } else { return client.Subscribe(channels...) } }
func Publish(channel string, message interface{}) *redis.IntCmd { if cfg.Cluster { return redisClusterClient.Publish(channel, message) } else { return client.Publish(channel, message) } }
|
redis_client.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package redis
import ( "github.com/go-redis/redis" "go-study/src/document" "go-study/src/log" "go.uber.org/zap" )
var client *redis.Client
var redisClusterClient *redis.ClusterClient
var cfg configRedis
func init() {
if err := document.Properties.Decode(&cfg); err != nil { log.LmdbLogger.Error("read document fail", zap.Error(err)) }
if cfg.Cluster { redisClusterClient = redis.NewClusterClient(&redis.ClusterOptions{ Password: cfg.Password, Addrs: cfg.Addrs, }) ping, err := redisClusterClient.Ping().Result() log.LmdbLogger.Info("Redis Ping", zap.String("ping", ping), zap.Error(err))
} else { client = redis.NewClient(&redis.Options{ Addr: cfg.Addr, Password: cfg.Password, DB: cfg.DB, }) pong, err := client.Ping().Result() log.LmdbLogger.Info("Redis Ping", zap.String("pong", pong), zap.Error(err)) }
}
type configRedis struct { Addr string `properties:"redis.addr"` Password string `properties:"redis.password"` DB int `properties:"redis.db,default=0"` Cluster bool `properties:"redis.cluster,default=false"` Addrs []string `properties:"redis.addrs,default=localhost:2181;localhost:2182"` }
|
demo_redis.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package redis
import ( "github.com/go-redis/redis" "go-study/src/log" "go.uber.org/zap" )
func DemoRedis() { log.LmdbLogger.Info("------redis---------") err := client.Set("key", "value", 0).Err() if err != nil { log.LmdbLogger.Error("client.Set fail", zap.Error(err)) }
val, err := Get("key").Result() if err != nil { log.LmdbLogger.Error("client.Get fail", zap.Error(err)) } log.LmdbLogger.Info("client.Get", zap.String("val", val))
val2, err := Get("key2").Result() if err == redis.Nil { log.LmdbLogger.Info("key2 does not exist") } else if err != nil { log.LmdbLogger.Error("client.Get fail", zap.Error(err)) } else { log.LmdbLogger.Info("client.Get", zap.String("val2", val2)) } sub := Subscribe("channel") go func() { for { pub := Publish("channel", "message") if pub.Err() != nil { log.LmdbLogger.Error("Publish err", zap.String("message", "message"), zap.Error(pub.Err())) } else { log.LmdbLogger.Info("Publish msg", zap.String("message", "message")) } } }()
msg, err := sub.ReceiveMessage() if err != nil { log.LmdbLogger.Error("Subscribe err", zap.Error(err)) } else { log.LmdbLogger.Info("Subscribe msg", zap.String("msg", msg.String())) }
}
|