GetWithCache makes a GET request to the Proxmox API with caching.
(path string, result *map[string]interface{}, ttl time.Duration)
| 158 | |
| 159 | // GetWithCache makes a GET request to the Proxmox API with caching. |
| 160 | func (c *Client) GetWithCache(path string, result *map[string]interface{}, ttl time.Duration) error { |
| 161 | // Generate cache key based on API path |
| 162 | cacheKey := fmt.Sprintf("proxmox_api_%s_%s", c.baseURL, path) |
| 163 | cacheKey = strings.ReplaceAll(cacheKey, "/", "_") |
| 164 | |
| 165 | // Try to get from cache first |
| 166 | var cachedData map[string]interface{} |
| 167 | |
| 168 | found, err := c.cache.Get(cacheKey, &cachedData) |
| 169 | if err != nil { |
| 170 | c.logger.Debug("Cache error for %s: %v", path, err) |
| 171 | } else if found { |
| 172 | c.logger.Debug("Cache hit for: %s", path) |
| 173 | |
| 174 | if result != nil { |
| 175 | // Copy the cached data to the result |
| 176 | *result = make(map[string]interface{}, len(cachedData)) |
| 177 | for k, v := range cachedData { |
| 178 | (*result)[k] = v |
| 179 | } |
| 180 | |
| 181 | return nil |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Cache miss or error, make the API call |
| 186 | c.logger.Debug("Cache miss for: %s", path) |
| 187 | |
| 188 | err = c.Get(path, result) |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | |
| 193 | // Cache the result |
| 194 | if result != nil && *result != nil { |
| 195 | if err := c.cache.Set(cacheKey, *result, ttl); err != nil { |
| 196 | c.logger.Debug("Failed to cache API result for %s: %v", path, err) |
| 197 | } else { |
| 198 | c.logger.Debug("Cached API result for %s with TTL %v", path, ttl) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | // GetWithRetry makes a GET request with retry logic and timeout. |
| 206 | func (c *Client) GetWithRetry(path string, result *map[string]interface{}, maxRetries int) error { |