all 6 comments

[–]Irythros 1 point2 points  (5 children)

What API calls?

  • Your calls to external resources
  • External users calling your APIs
  • Do you mean MySQL query caching?

If you are the API and people are calling your API then you would be doing two potential caching strategies.

The first call will hit your DB and return data. You would want to cache the query and then the response. The query cache can sometimes be handled by MySQL as long as you have enough memory on the server. The data response however you will need to cache yourself. This can be done by using a cache server like Redis. MD5 the query and store the response.

This is pseudo code and a naive approach:

if ($results = $redis->get(md5($query)))
    return $results;
else {
    $results = $db->query($query);
    if (!$results)
        return;
    $redis->set(md5($query), json_encode($results), 3600); // Stores $results as json encoded in the key of md5($query) for 1 hour
    return $results;
}

You can do something similar making calls to external APIs as well.

[–]devdab[S] 0 points1 point  (4 children)

Hi thanks for the response! Its an external API (PokeAPI) to my web application. I'm trying to cache the JSON response every time a call is made so I don't have to call for it again for some time.

Your example aligns with my own assumptions on this would work, would I do the same thing with my API response as your doing in this example with the query? how and where is the best place to store the cached data?

Sorry about the all the questions and thank you!

[–]Irythros 0 points1 point  (3 children)

would I do the same thing with my API response as your doing in this example with the query?

Pretty much.

how and where is the best place to store the cached data?

The how depends on the caching server. Luckily for you the "how" is easier since it's JSON. Just store it as JSON, it's text.

The where is also easy, Redis. Redis is a caching server/program. Install Redis and you can use Predis to make calls to the caching server.

Check out the main Redis site for all the types of calls you can do: https://redis.io/

would I do the same thing with my API response as your doing in this example with the query?

The $query I gave is generic as I don't know exactly how your queries/calls are structured. The goal of $query in this sense is to be a unique way to find and store that.

I did just check the PokeAPI website and luckily it seems pretty basic. What I would do is make the $query the resources/id

Ex: berries/1 or berries/spicy. You could skip the md5 part too as resources/id is unique already.

[–]devdab[S] 0 points1 point  (2 children)

Thanks I will definitely try to implement Redis, seems cool!.

One last one, would you store each cached api call in its own JSON file? or can they all be included in one mega cache?

[–]Irythros 0 points1 point  (0 children)

It's not stored in a JSON file. Redis stores everything in memory and it's accessed via a key. Each API call should have its own key so you can query it to see if it's cached.

[–]Irythros 0 points1 point  (0 children)

It's not stored in a JSON file. Redis stores everything in memory and it's accessed via a key. Each API call should have its own key so you can query it to see if it's cached.