| 29 | } |
| 30 | |
| 31 | func (c *relationCache) GetCache(ctx context.Context, key string) ([]domain.Relation, error) { |
| 32 | // 从Redis获取数据 |
| 33 | data, err := c.client.Get(ctx, key).Result() |
| 34 | if errors.Is(err, redis.Nil) { |
| 35 | return nil, nil // 缓存未命中 |
| 36 | } else if err != nil { |
| 37 | return nil, err // Redis调用失败 |
| 38 | } |
| 39 | |
| 40 | // 解码数据 |
| 41 | var relations []domain.Relation |
| 42 | |
| 43 | if err := json.Unmarshal([]byte(data), &relations); err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | return relations, nil |
| 48 | } |
| 49 | |
| 50 | func (c *relationCache) SetCache(ctx context.Context, key string, relations []domain.Relation, ttl time.Duration) error { |
| 51 | // 编码数据为JSON格式 |