Expire the stats cache once per day

When we first started caching the stats, generating them was a process
that took several minutes, so we never expired the cache.

However, there have been cases where we run into issues where the stats
shown on the screen were outdated. That's why we introduced a task to
manually expire the cache.

But now, generating the stats only takes a few seconds, so we can
automatically expire them every day, remove all the logic needed to
manually expire them, and get rid of most of the issues related to the
cache being outdated.

We're expiring them every day because it's the same day we were doing in
public stats (which we removed in commit 631b48f58), only we're using
`expires_at:` to set the expiration time, in order to simplify the code.

Note that, in the test, we're using `travel_to(time)` so the test passes
even when it starts an instant before midnight. We aren't using
`:with_frozen_time` because, in similar cases (although not in this
case, but I'm not sure whether that's intentional), `travel_to` shows
this error:

> Calling `travel_to` with a block, when we have previously already made
> a call to `travel_to`, can lead to confusing time stubbing.
This commit is contained in:
Javi Martín
2024-04-18 22:16:26 +02:00
parent a5646fcdb3
commit a4461a1a56
13 changed files with 45 additions and 84 deletions

View File

@@ -122,6 +122,6 @@ class Poll::Stats
stats_cache(*stats_methods)
def full_cache_key_for(key)
"polls_stats/#{poll.id}/#{key}/#{version}"
"polls_stats/#{poll.id}/#{key}"
end
end