Hacker News new | ask | show | jobs
by Izkata 2955 days ago
This is similar to what I was thinking, but in python and remaining more object-oriented, its API would look something like this: (with essentially the same invalidation logic in the init)

   cache = GroupedCache(key="dashboard_stats")
   print("Total users: {}".format(cache.get(countUsers))
   print("Articles written: {}".format(cache.get(countArticles))
   print("Words written: {}".format(cache.get(countWords))
To me this would make it more apparent when reading the code that these should all be cached together (your functional one, with a different cache strategy, makes it look like it's acceptable for them to be cached for 1 hour, but offset from 30 minutes from each other due to external circumstances), and while pretty irrelevant for this particular example, would avoid the miniscule race condition if the clock ticks over midnight between Total Users and Articles Written.