我有这个(不完整的)示例程序使用go-redis:
func doThings(ctx context.Context, cache redis.Cache) {
..
eventString, err := cache.GetDel(ctx, myKey).Result()
..
}
func doOtherThings(ctx context.Context, cache redis.Cache) {
..
eventString, err := cache.GetDel(ctx, myKey).Result()
..
}
func main() {
ctx := context.Background()
cache := redis.NewClient(&redis.Options{
Addr: redisHost,
Password: "", // no password set
DB: redisDatabase, // use default DB
})
go doThings(ctx, *cache)
go doOtherThings(ctx, *cache)
}
创建一个 Client 并将对它的引用传递给不同的 go 例程是否有任何问题?这两个例程都可以执行几个
Get
,Set
和Del
操作。
我不担心执行顺序,但会不会引起奇怪的副作用,例如放弃对 redis 的调用,或者如果我创建大量的 go 例程会阻塞太多?
或者,我应该为每个 go 例程创建一个客户端吗?
同样的问题是使用 kafka 阅读器和编写器 .