Set 设置缓存
(ctx context.Context, key string, value any, ttl time.Duration)
| 161 | |
| 162 | // Set 设置缓存 |
| 163 | func (c *ToolCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error { |
| 164 | if !c.config.Enabled { |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | c.stats.Sets++ |
| 169 | |
| 170 | // 设置到内存缓存 |
| 171 | if c.config.Strategy == CacheStrategyMemory || c.config.Strategy == CacheStrategyBoth { |
| 172 | c.setToMemory(key, value, ttl) |
| 173 | } |
| 174 | |
| 175 | // 设置到文件缓存 |
| 176 | if c.config.Strategy == CacheStrategyFile || c.config.Strategy == CacheStrategyBoth { |
| 177 | if err := c.setToFile(key, value, ttl); err != nil { |
| 178 | c.stats.Errors++ |
| 179 | return fmt.Errorf("failed to set file cache: %w", err) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // Delete 删除缓存 |
| 187 | func (c *ToolCache) Delete(key string) error { |