Skip to content

Add "cache identifier" argument to .GetRemote #9755

@regisphilibert

Description

@regisphilibert

Currently users grabbing data from an API have to rely on file caching to ensure the API is not solicited on every build. (To avoid limitations but also slower builds)
To refresh the response every hour, set up a hourly build and set your cache settings to:

caches:
  getresource:
    maxAge: 1h

Then file caching setting of Hugo identifies a cached "response" with a hashed concatenation of the request's URL and the passed options (headers, body etc...)

This presents several problem.

  1. Some API requires the date and time to be included in the authorization header for extra security. This means the .GetRemote options will always be different thus producing a unique cache identifier every time.
  2. Cache options are shared by ever request. Every .GetRemote call will be subject to the same maxAge for example. No way to set one API call to refresh every day, and another one every hour.

Proposal

Add a third and optional parameter to .GetRemote to pass on a cache identifier.

Not only will it solves 1. by giving control to users on the cache identifier (omitting some options), but it will also give more control to users, thus bypassing the 2. limitation.

Cache for approximately one hour by including the current time (to the hour) in the cache id:

$cache_id := print $url (time.Format "2006-01-02T15")
$request := resource.GetRemote $url $options $cache_id

Cache for approximately one day by including the current date (to the day) in the cache id:

$cache_id := print $url (time.Format "2006-01-02")
$request := resource.GetRemote $url $options $cache_id

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions