HTTP Cache maxAge by url pattern

Hello guys,

I’m using resources.GetRemote to fetch json data from API and then I’m using very same resources.GetRemote to get images.

Is there any way to have different cache time per url pattern, so images could be cached ‘forever’?

Thanks in advance.

https://gohugo.io/functions/resources/getremote/#caching

are you suggesting setting maxAge: -1 globally and then using cache key for important stuff?

Yes, I am.

But then you can’t control actual age of cache. I could use something like modulus of time.Unix, but it is not precise. It only creates timeframes.

The opposite way seems more viable. I can set maxAge globally to reasonable value and set cache key to something like $url+forever for images. And it won’t be creating as much cache files.

Thank you for pushing me into the right direction.

well, turns out that it is not working as intended. When I set maxAge to 60 and then override this with custom cache key, all resources are downloaded each build.

If you by this mean maxAge=60 that means “60 milliseconds”, which is … not long.

Try

maxAge="60m"

Or something.

I’m not sure if this would solve your particular problem, but you could have a look at

You could e.g. enable the HTTP cache behaviour for your JSON resources, which would effectively override what’s defined in the file cache (if the file has changed, it gets evicted).

Thank you so much! Now it seems to work. I have set maxAge to -1 and enabled HTTP cache behavior for JSON. So images are cached forever even without keys. And JSON is respecting cache-control headers (I guess?)

1 Like

You can use the “key” in the resources.GetRemote options map to force an update before the max age has expired, but you can’t override the max age.

1 Like

That’s just about right.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.