Update cached JSON data

When getting JSON from a URL (as described here), it is cached locally (which is great). Wondering how / when this cache is updated, and if it’s possible to force that. You can force the use of live data with --ignoreCache, but the cache remains at the previous version. It would be useful to be able to say “get a fresh version of the data, now cache that and give me the (newly) cached version on subsequent requests”.

1 Like

You need to atop thinking of Hugo as a dynamic website creation system and step one step back. It is explained in your links… call hugo with --cache-dir=somethingdynamic123 and each time you run it the cache is newly created. Also if you tell Hugo where to cache files you can remove the cached files before your next hugo run.

What I am saying is that the handling of the cache (where is the cache, use the old cache) is done per hugo run. So your creational logic outside of that run needs to know if it is a re-run or a new-run.

If you won’t use the cache on subsequent runs then move the directory per hugo-run. Like having a cache folder with a timestamp attached to the name.

See https://gohugo.io/getting-started/configuration/#configure-file-caches

If you set the maxAge to 30s (30 seconds) or something, you should get close to what you want.

Not quite @bep, I don’t want to be calling the API that often, just every once in a while, when I know the data has changed. I would expect that temporarily setting maxAge to 0 would achieve what I’m looking for, but that is not the case. Removing maxAge seems to revert to the previously cached version.

@davidsneighbour your suggestion of setting the location then deleting the file as necessary sounds like the closest to what I’m looking for. (Not ideal, due to having to put an explicit path in the config.)

Coming back to the question of defaults. The docs say the default is to cache forever (maxAge = -1). I’ve seen my data update from day to day though. So that’s still not clear to me.

I’m trying to achieve the same behaviour (for retrieve stock informations every hour, but potentially refreshable at any time by client) and I’m thinking our use case is broad enough to consider its requirements. A lot of people need to limit API calls, while still needing some occasional (and unforseeable) refresh.

Maybe a simple --refreshCache CLI flag could be added? It would refresh the cache with a new response regardless of maxAge but woud cache new fetched data for the config’s maxAge length.

In the mean time I was entertaining the idea of creating a special Hugo Environment (nocache or something) whose cache’s maxAge settings would be set to 1min.
Upon triggered, this Env’s built would set the cache to one minute, meaning the next build being triggered under the regular environment, would not be able to use this short lived cache and woudl have to create a new one (cached for 1 hours for ex). We still end up with 2 API calls withing a few minutes, but I don’t see any current workaround.

1 Like