GetCache returns a cached pricing response for the given key if not expired.
(cacheKey string)
| 173 | |
| 174 | // GetCache returns a cached pricing response for the given key if not expired. |
| 175 | func (d *DB) GetCache(cacheKey string) (*api.PricingAPIResponse, error) { |
| 176 | cutoff := time.Now().UTC().Add(-time.Duration(cacheTTLDays) * 24 * time.Hour).Format(time.RFC3339) |
| 177 | var jsonStr string |
| 178 | err := d.conn.QueryRow( |
| 179 | "SELECT response_json FROM pricing_cache WHERE cache_key = ? AND created_at > ?", |
| 180 | cacheKey, cutoff, |
| 181 | ).Scan(&jsonStr) |
| 182 | if err == sql.ErrNoRows { |
| 183 | return nil, nil |
| 184 | } |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | var resp api.PricingAPIResponse |
| 189 | if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil { |
| 190 | return nil, err |
| 191 | } |
| 192 | return &resp, nil |
| 193 | } |
| 194 | |
| 195 | // SetCache stores or replaces a pricing response in the cache. |
| 196 | func (d *DB) SetCache(cacheKey string, resp *api.PricingAPIResponse) error { |