MCPcopy Create free account
hub / github.com/astercloud/aster / Load

Method Load

pkg/memory/preference_storage.go:73–97  ·  view source on GitHub ↗

Load 实现 PreferenceStorage 接口

(
	ctx context.Context,
	userID string,
)

Source from the content-addressed store, hash-verified

71
72// Load 实现 PreferenceStorage 接口
73func (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 接口
100func (fs *FilePreferenceStorage) Delete(ctx context.Context, userID string) error {

Calls 2

JoinMethod · 0.45
ReadFileMethod · 0.45