So, since the post didn’t gather a constructive discussion, I tried to implement something like this on my own.
What I did was implementing template functions called getCachedJSON
and getCacheCSV
that act as a wrapper around getJSON
and getCSV
and take an extra integer as a first parameter.
This first parameter is the time-to-live in seconds for the data cache. That means upon loading the content for the first time, it will be stored for ttl
seconds. When you try to access it again, if ttl
seconds has passed, the cached entry will be purged and getJSON
/getCSV
will be called to fetch fresh content.
Of course, if --ignoreCache
is passed to hugo, cache will be dismissed in any case.
This option is I believe useful when rebuilding your site rather often and you don’t necessarily want to fetch external content every time (which could raise anti-spam/anti-DDOS measures).
I’m currently unable to run the tests I carelessly wrote. It says Skip Check on go1.8.1
although I upgraded my golang setup and go version
says go version go1.10 linux/amd64
. Sorry about that, but it’s my first time dealing with go and I’m not really comfortable with the toolset just yet. If you have any idea how to address this issue, let me know
However, I did some template-side testing with Wireshark in the background and it appeared to have the desired effect of sending network requests only after ttl
seconds has passed (after the first query has been completed, of course).
Example usage:
{{ $days := getCachedJSON 3600 $url }}
This would keep the content of $url for one hour throughout as many builds as you can imagine.
What do you think about this? Is this a desired feature? Am I implementing it properly? All comments welcome