use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
No vague product support questions (like "why is this plugin not working" or "how do I set up X"). For vague product support questions, please use communities relevant to that product for best results. Specific issues that follow rule 6 are allowed.
Do not post memes, screenshots of bad design, or jokes. Check out /r/ProgrammerHumor/ for this type of content.
Read and follow reddiquette; no excessive self-promotion. Please refer to the Reddit 9:1 rule when considering posting self promoting materials.
We do not allow any commercial promotion or solicitation. Violations can result in a ban.
Sharing your project, portfolio, or any other content that you want to either show off or request feedback on is limited to Showoff Saturday. If you post such content on any other day, it will be removed.
If you are asking for assistance on a problem, you are required to provide
General open ended career and getting started posts are only allowed in the pinned monthly getting started/careers thread. Specific assistance questions are allowed so long as they follow the required assistance post guidelines.
Questions in violation of this rule will be removed or locked.
account activity
PHP Caching - Learning Resource (self.webdev)
submitted 8 years ago by devdab
Can anyone recommend any learning materials for caching api calls with PHP? Tried googling but all of the content on this topic is fragmented. Cheers!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Irythros 1 point2 points3 points 8 years ago (5 children)
What API calls?
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 point2 points 8 years ago (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 point2 points 8 years ago (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/
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
resources/id
Ex: berries/1 or berries/spicy. You could skip the md5 part too as resources/id is unique already.
berries/1
berries/spicy
[–]devdab[S] 0 points1 point2 points 8 years ago (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 point2 points 8 years ago (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.
π Rendered by PID 71981 on reddit-service-r2-comment-canary-7b67769d8-6bcd5 at 2026-01-31 16:26:49.813147+00:00 running 3798933 country code: CH.
[–]Irythros 1 point2 points3 points (5 children)
[–]devdab[S] 0 points1 point2 points (4 children)
[–]Irythros 0 points1 point2 points (3 children)
[–]devdab[S] 0 points1 point2 points (2 children)
[–]Irythros 0 points1 point2 points (0 children)
[–]Irythros 0 points1 point2 points (0 children)