template-monorepo/internal/library/redis/README.md

24 lines
871 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# RedisGateway 共用連線)
## 用途
-**`svc.ServiceContext` 建立一次** `*redis.Client`,全進程共用 go-zero 的 connection pool同一 `Addr` 只會有一個 pool
-`internal/model/{module}` 的 factory / repository **注入**此 client禁止在 module 內 `go-redis.NewClient` 或重複 `MustNewRedis`
## 使用
```go
import redislib "gateway/internal/library/redis"
rds, err := redislib.NewClient(c.Redis) // Host 空 → (nil, nil)
rds.Zero() // *github.com/zeromicro/go-zero/core/stores/redis.Redis
```
## 與 Mongo 對照
| | Mongo | Redis |
|---|--------|--------|
| 封裝 | `library/mongo` | `library/redis` |
| 共用機制 | go-zero `mon.clientManager`key = URI | go-zero `redis.clientManager`key = Addr |
| 模組內 | 每 collection 一個 repository | 共用 client + 模組 `redis.go` 定義 key |