Setting maxAge on JSON resources

Am fetching using resources.GetRemote to get both JSON and also images. The resources are cache by setting file cache on `getresource’.

Wonder if there is a way maxAge on resources by their filetype. I would like JSON have maxAge: 6h wherease other resources to be cached with maxAge : -1

{{ with resources.GetRemote "https://archive.org/advancedsearch.php?q=collection%3AServantsOfKnowledge+(language%3Aori+OR+language%3A%22Oriya%22)&fl%5B%5D=collection&fl%5B%5D=addeddate&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=title&fl%5B%5D=description&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=20&page=1&output=json" }}
  {{ with . | unmarshal }}
    {{ with .response.docs }}
     {{ warnf "%v" .}}
    {{ end }}
  {{end}}
{{end}}

My config.yaml has following cache settings.

  getresource:
    dir: :resourceDir/_gen
    maxAge: -1

Are you going to rebuild your site every 6 hours?

This will generate a new cache key at 6 am, noon, 6 pm, and midnight.

{{ $data := dict }}
{{ $url := "https://www.example.com/data/books.json" }}
{{ $cacheKey := print $url (now.Format "20060102") (div now.Hour 6) | md5 }}
{{ with resources.GetRemote $url (dict "key" $cacheKey) }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ $data = . | unmarshal }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %s" $url }}
{{ end }}

See https://gohugo.io/hugo-pipes/introduction/#caching

1 Like

Thank you! That’s perfect.

The objective is not to send too many requests to API service. We would like to get fetch the JSON to check if any new resources are added, if so get the resource content and resource image thumbnail.

It also helps run builds faster as well.

Hold off. I just realized the same key will be produced today at noon, tomorrow at noon, etc.

Do this instead:

{{ $cacheKey := print $url (now.Format "20060102") (div now.Hour 6) | md5 }}
1 Like

Thank you once again @jmooring

1 Like

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