MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / Get

Method Get

internal/repository/cache/user.go:31–58  ·  view source on GitHub ↗

Get 从redis中获取数据并反序列化

(ctx context.Context, uid int64)

Source from the content-addressed store, hash-verified

29
30// Get 从redis中获取数据并反序列化
31func (u *userCache) Get(ctx context.Context, uid int64) (domain.User, error) {
32 if uid <= 0 {
33 return domain.User{}, fmt.Errorf("无效的用户ID: %d", uid)
34 }
35
36 var du domain.User
37 key := fmt.Sprintf("linkeme:user:%d", uid)
38
39 // 从redis中读取数据
40 data, err := u.cmd.Get(ctx, key).Result()
41 if err != nil {
42 if err == redis.Nil {
43 return domain.User{}, fmt.Errorf("缓存中未找到用户 %d", uid)
44 }
45 return domain.User{}, fmt.Errorf("从缓存获取用户失败: %v", err)
46 }
47
48 if err = json.Unmarshal([]byte(data), &du); err != nil {
49 return domain.User{}, fmt.Errorf("反序列化用户数据失败: %v", err)
50 }
51
52 // 如果用户已被删除,则不返回数据
53 if du.Deleted {
54 return domain.User{}, fmt.Errorf("用户 %d 已被删除", uid)
55 }
56
57 return du, nil
58}
59
60// Set 将传入的du结构体序列化存入redis中
61func (u *userCache) Set(ctx context.Context, du domain.User) error {

Callers

nothing calls this directly

Calls 1

GetMethod · 0.65

Tested by

no test coverage detected