Load 实现 PreferenceStorage 接口
( ctx context.Context, userID string, )
| 71 | |
| 72 | // Load 实现 PreferenceStorage 接口 |
| 73 | func (fs *FilePreferenceStorage) Load( |
| 74 | ctx context.Context, |
| 75 | userID string, |
| 76 | ) ([]*Preference, error) { |
| 77 | fs.mu.RLock() |
| 78 | defer fs.mu.RUnlock() |
| 79 | |
| 80 | // 读取文件 |
| 81 | filePath := filepath.Join(fs.dir, userID+".json") |
| 82 | data, err := os.ReadFile(filePath) |
| 83 | if err != nil { |
| 84 | if os.IsNotExist(err) { |
| 85 | return []*Preference{}, nil // 文件不存在返回空列表 |
| 86 | } |
| 87 | return nil, fmt.Errorf("failed to read file: %w", err) |
| 88 | } |
| 89 | |
| 90 | // 反序列化 |
| 91 | var preferences []*Preference |
| 92 | if err := json.Unmarshal(data, &preferences); err != nil { |
| 93 | return nil, fmt.Errorf("failed to unmarshal preferences: %w", err) |
| 94 | } |
| 95 | |
| 96 | return preferences, nil |
| 97 | } |
| 98 | |
| 99 | // Delete 实现 PreferenceStorage 接口 |
| 100 | func (fs *FilePreferenceStorage) Delete(ctx context.Context, userID string) error { |