@@ -208,7 +208,7 @@ type FriendDatabase interface {
208208 CheckIn (ctx context.Context , user1, user2 string ) (inUser1Friends bool , inUser2Friends bool , err error )
209209 // ...
210210
211- // 新增 AddFriendCategory 接口
211+ // 定义 Controller 层的 AddFriendCategory 接口
212212 AddFriendCategory (ctx context.Context , userID, categoryName string ) error
213213}
214214
@@ -231,7 +231,7 @@ func (f *FriendDatabase) AddFriendCategory(ctx context.Context, userID, category
231231在 ` pkg/common/storage/cache ` 中增加新的接口,在 ` pkg/common/storage/cache/cachekey ` 中实现对应的 Key,并实现对应的接口,提供给 controller 层调用。
232232
233233例如我们定义的 ` AddFriendCategory ` 接口,需在 ` pkg/common/storage/cache/cachekey/friend.go ` 中实现其前缀和对应的 Get 函数,
234- 在 ` pkg/common/storage/cache/friend.go ` 定义供 controller 层调用的接口,并在 ` pkg/common/storage/cache/redis/friend.go ` 实现对应的缓存逻辑。
234+ 再 ` pkg/common/storage/cache/friend.go ` 定义供 controller 层调用的接口,并在 ` pkg/common/storage/cache/redis/friend.go ` 实现对应的缓存逻辑。
235235
236236** cachekey/friend.go**
237237
@@ -254,7 +254,7 @@ type FriendCache interface {
254254 CloneFriendCache () FriendCache
255255 // ...
256256
257- // 新增 AddFriendCategory 接口
257+ // 定义 Cache 层的 AddFriendCategory 接口
258258 AddFriendCategory (ctx context.Context , userID, categoryName string ) error
259259}
260260```
@@ -271,7 +271,44 @@ func (f *FriendCacheRedis) AddFriendCategory(ctx context.Context, userID, catego
271271
272272### 添加 database 层接口
273273
274- - 在 pkg/common/storage/model 中可定义数据库的 model 结构体,pkg/common/storage/database 中增加新的接口,实现对应的接口,提供给 cache 层整合。
274+ 在 ` pkg/common/storage/model ` 中,定义对应数据库的 model 结构体,然后在 ` pkg/common/storage/database ` 中增加新的接口,并实现对应的接口,提供给 cache 层整合。
275+
276+ 例如,我们定义的 ` AddFriendCategory ` 接口,需要在 ` pkg/common/storage/model/friend.go ` 中定义对应的 model 结构体,
277+ 然后在 ` pkg/common/storage/database/friend.go ` 中添加对应的接口供 cache 层整合,在 ` pkg/common/storage/database/mgo/friend.go ` 中实现对应的数据库操作。
278+
279+ ** model/friend.go**
280+
281+ ``` go
282+ type Friend struct {
283+ ID primitive.ObjectID ` bson:"_id"`
284+ OwnerUserID string ` bson:"owner_user_id"`
285+ // ...
286+ CategoryName string ` bson:"category_name"` // 新增 CategoryName 字段
287+ }
288+ ```
289+
290+ ** database/friend.go**
291+
292+ ``` go
293+ type Friend interface {
294+ UpdateRemark (ctx context.Context , ownerUserID, friendUserID, remark string ) (err error )
295+ // ...
296+ // 定义 DB 层的 AddFriendCategory 接口
297+ AddFriendCategory (ctx context.Context , userID, categoryName string ) error
298+ }
299+ ```
300+
301+ ** database/mgo/friend.go**
302+
303+ ``` go
304+ func (f *FriendMgo ) AddFriendCategory (ctx context .Context , userID , categoryName string ) error {
305+ // 实现对应的数据库操作
306+ // ...
307+
308+ return nil
309+ }
310+
311+ ```
275312
276313# 客户端
277314
0 commit comments