Hello,
I’m currently utilizing the Google Places API. However, I find myself repeatedly making the same API calls across different sections. For instance, I call the API separately for reviews, and then again in the footer to retrieve address and opening hours. Is there a more efficient method to globally make API calls, cache the data, and then access it across various sections without redundantly fetching the same data?
# here is the code example
{{ $googleReviews := .Site.Params.widget.reviews.google }} {{ $apikey :=
$googleReviews.api_key }} {{ $place_id := $googleReviews.place_id }} {{ $url :=
printf
"https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s&language=el"
$place_id $apikey }} {{ $data := getJSON $url }} {{ with index $data "result" }}
Thank you.
First, getJSON
has been deprecated. Use resources.GetRemote
instead. This method takes an optional cache key so you can uniquely invalidate the cache for each call.
Second, if you’re not already doing so, use cached partials.
Finally, depending on when/where you make your calls, you might be able to stuff the result into a scratch pad using the Store
method on a Page
object, then retrieve it as needed.
1 Like
Hi @jmooring
I have not understand this I spend many months to understand the above code and now it is deprecated. What I need to change to be able to use {{ .rating }} in multiple locations without copying the whole code again and again
# here is the code example in reviews page
{{ $googleReviews := .Site.Params.widget.reviews.google }} {{ $apikey :=
$googleReviews.api_key }} {{ $place_id := $googleReviews.place_id }} {{ $url :=
printf
"https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s&language=el"
$place_id $apikey }} {{ $data := getJSON $url }} {{ with index $data "result" }}
{{ .rating }}
{{end}}
then using the same code on contact page
# contact page
{{ $googleReviews := .Site.Params.widget.reviews.google }} {{ $apikey :=
$googleReviews.api_key }} {{ $place_id := $googleReviews.place_id }} {{ $url :=
printf
"https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s&language=el"
$place_id $apikey }} {{ $data := getJSON $url }} {{ with index $data "result" }}
{{ .rating }}
{{end}}
But I wonder if on contact page I can simply call
# some sort of Hugo cache for something to only use
{{ .rating }}
Thank you
I believe I answered all of your questions in my previous reply.
What’s the issue in replacing getJSON
with getRemote
? The documentation explains how to get from that to a dict
analogous to what you got before with getJSON
. Pass an additional key to getRemote
so that it’s called only once for each key.
Aside: Formatting your code so that each statement occupies one line helps readability and understanding.
Hi @chrillek
I do not understand now, my brain have given up on Hugo I spent so much time learning something which is deprecated and now what @jmooring wrote above sounds Chinese to me can’t make sense what the documentation is telling me to do?
Thanks
Hi @jmooring
I spent so much time and still not able to understand how this cache work? if you can please convert the below code so I do not have to make the same call agains and again and simply use the data from json on different page. how will this code looks like using your technique?
Thanks
@Papastratos
Your original account, @gora, was administratively silenced until Apr 1, 2024 8:00 am. Your current account is what’s known as a sock puppet. Your current account is now suspended. Please wait until your original account is un-silenced on Apr 1.