Hacker News new | ask | show | jobs
by gchq-7703 1299 days ago
> I want to cache a guide until it changes, but update the exchange rates every few hours.

We've found that NextJS's Incremental Static Regeneration[0] hits the sweet spot for when we have needed to do this. Specifying a regeneration time allows us to say 'this data can be at most X seconds old' but if thousands of users hit a URL at once only one request is made.

[0] https://nextjs.org/docs/basic-features/data-fetching/increme...

2 comments

I think that the page cache is set to 1 day, and dropping it to 12 hours would not strain the server. I'd just save the rates to a file on the server, and create a twig filter that wraps euro amounts with the exchange rate data. I already do this to replace constants (BLUE_CARD_MIN_INCOME) with values from a file.

However server-side generation would mean that exchange rates are cached along with the page by Google and archive.org. An old version of the page would show old exchange rates.

I care a great deal about letting users consume the content even if the website goes down, either through archive.org, Google Cache, CloudFlare, Pocket or their own archives.

Sorry to be that guy but man cron - periodic execution is an (elegantly) solved problem in the unix world, since 1975. https://en.wikipedia.org/wiki/Cron (And man stat for file age. No DB necessary.)

Particularly today when so many programmers accept re-implementation of systems primitives in higher level languages de jour, there exists a wry truth in the (admittedly somewhat condescending) adage:

Those who don't understand Unix are condemned to reinvent it, poorly. - Henry Spencer

This is hilarious. Do you really propose to rebuild front end application every hour, tens to hundreds of pages, just to get the new exchange rate in the app? New tools have new ways to solve things, and this is the case where the new way makes much more sense - it is built in the stack, it scales well, and it solves the problem.

This sounds like an old man with a hammer, reminiscing of times where every single problem was a nail. True, hammer works for the threaded wood screws somewhat, but the new engineers with their electric screwdrivers are more efficient and build sturdier things.

Even if you are hell-bent on doing stuff the Linux way why not propose a pair of Systemd service and timer? At least there will be logs. Cron is such an antiquated way of doing things.

The discussion was regarding periodic updates. I think you are mis-reading it. I am pointing out periodic execution logic does not need to be in your request handling or app logic, you are well advised to leave it as a separate task and schedule it. cron is the traditional way. You can do it another way. But you know, use a small piece of code with no dependencies and a clearly defined scope and purpose. Use the filesystem. Use a file. Dates come free. Readable in all environments. Very manageable. Simple is best. Maybe you haven't had that epiphany yet? It's a good one.