tidbyt / pixlet

Build apps for pixel-based displays ✨
https://tidbyt.com
Apache License 2.0
756 stars 107 forks source link

Remove TTL header from cache key #1048

Closed dinosaursrarr closed 4 months ago

dinosaursrarr commented 4 months ago

See discussion on Discord. 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.

matslina commented 4 months ago

I believe the main motivation for including TTL in cache key was to protect against apps accidentally using a giant TTL, which would then leave the developer unable to have a fix (with lower TTL) take effect until the (giant) TTL has expired. With TTL in cache key, changing the TTL is equivalent to evicting the old record, from the app's perspective.

But I think we can do this. I don't recall ever seeing anyone accidentally using too large a TTL, but plenty of people have wanted to dynamically change TTL without read-through.

dinosaursrarr commented 4 months ago

Seems like you could also introduce a special header on the request to overwrite any existing cache entries in case the giant TTL risk became a real problem

matslina commented 4 months ago

@dinosaursrarr I don't think we need the migration steps in here; would you mind updating the PR? The actual production cache code lives elsewhere, so it won't matter, and even if it did I think I'd prefer just getting the change out, verifying the world didn't break, and call it a day. =)

matslina commented 4 months ago

Seems like you could also introduce a special header on the request to overwrite any existing cache entries in case the giant TTL risk became a real problem

Yeah that's an option if it's ever needed.

dinosaursrarr commented 4 months ago

@matslina PR updated as requested

matslina commented 4 months ago

Many thanks!