GenerateKey 生成缓存键
(toolName string, input map[string]any)
| 115 | |
| 116 | // GenerateKey 生成缓存键 |
| 117 | func (c *ToolCache) GenerateKey(toolName string, input map[string]any) string { |
| 118 | // 序列化输入参数 |
| 119 | data, err := json.Marshal(input) |
| 120 | if err != nil { |
| 121 | // 如果序列化失败,使用简单的字符串拼接 |
| 122 | return fmt.Sprintf("%s_%v", toolName, input) |
| 123 | } |
| 124 | |
| 125 | // 计算 SHA256 哈希 |
| 126 | hash := sha256.Sum256(data) |
| 127 | return fmt.Sprintf("%s_%x", toolName, hash[:16]) // 使用前16字节 |
| 128 | } |
| 129 | |
| 130 | // Get 获取缓存 |
| 131 | func (c *ToolCache) Get(ctx context.Context, key string) (any, bool) { |
no outgoing calls