24 lines
871 B
Markdown
24 lines
871 B
Markdown
# Redis(Gateway 共用連線)
|
||
|
||
## 用途
|
||
|
||
- 在 **`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 |
|