Skip to content

Commit

Permalink
Remove TTL header from cache key (#1048)
Browse files Browse the repository at this point in the history
See [discussion on Discord](https://discord.com/channels/928484660785336380/928485908842426389/1231791730513412136). Multiple times, app developers have wanted to cache a piece of content until a particular time, rather than for a particular period. That requires setting ttl_seconds to a variable amount, calculated as the time remaining.

Currently, the cache client won't support that. It generates the cache key from the entire request, which includes the TTL seconds in a header. This change removes that header before generating the key. That seems sensible because the key should be about the content and how it would be fetched, but the TTL is about what to do with the response.

As a transitionary step, this writes to new TTL-less keys, but continues to look up keys with TTLs. Once all the old cache entries have expired, then we can stop looking up the old keys.
  • Loading branch information
dinosaursrarr authored Apr 24, 2024
1 parent 9083434 commit 452de9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions runtime/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
HTTPTimeout = 5 * time.Second
MaxResponseBytes = 20 * 1024 * 1024 // 20MB
HTTPCachePrefix = "httpcache"
TTLHeader = "X-Tidbyt-Cache-Seconds"
)

// Status codes that are cacheable as defined here:
Expand Down Expand Up @@ -106,10 +107,15 @@ func (c *cacheClient) RoundTrip(req *http.Request) (*http.Response, error) {
}

func cacheKey(req *http.Request) (string, error) {
ttl := req.Header.Get(TTLHeader)
req.Header.Del(TTLHeader)
r, err := httputil.DumpRequest(req, true)
if err != nil {
return "", fmt.Errorf("%s: %w", "failed to serialize request", err)
}
if ttl != "" {
req.Header.Set(TTLHeader, ttl)
}

h := sha256.Sum256(r)
key := hex.EncodeToString(h[:])
Expand Down
4 changes: 2 additions & 2 deletions runtime/testdata/httpcache.star
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main(config):

resp = http.get(
url = "https://example.com",
ttl_seconds = 60,
ttl_seconds = 3,
)
assert.eq(resp.headers.get("Tidbyt-Cache-Status"), "HIT")

Expand All @@ -32,7 +32,7 @@ def main(config):
url = "https://example.com",
ttl_seconds = 60,
)
assert.eq(resp.headers.get("Tidbyt-Cache-Status"), "MISS")
assert.eq(resp.headers.get("Tidbyt-Cache-Status"), "HIT")

resp = http.post(
url = "https://example.com",
Expand Down

0 comments on commit 452de9a

Please sign in to comment.