Caching a goole map api query

aaazz97

Junior Member
Joined
Aug 18, 2019
Messages
125
Reaction score
73
Hi there,

I'm building a directory on a website with google places api. I created a shortcode with a parameter, that I can put on every pages like [shortocde='my query']
Based on 'my query', I have a php script that use the google maps api to push the data well formated on the page.
Infos are : name, address, phone, website, photo
I've generated about 120 pages with 20 listing on each one.
I've also spent $500 a day on api cost and that's the main problem

How to cache each page to avoid doing api calls each time the page is visited ?
 
You already know the answer :) instead of calling the Google api you need to call your caching function sever side. Use the query as the cache key, check the cache for existing keys.. if no key is present call the api.. store the response in the cache and return the response. No php expert but I believe apc_store() will do what you need.
 
Exactly as @Smackhead said. Store your queries as a cache in the database. If the results are too big or there are too many queries, then store them in the file system to avoid slowing down the site by cluttering the database. This is what most cache plugins do, they use the file system.

As Smackhead also mentioned, use the query parameters as the cache key to make it unique and the system checks if it already exists. If not, then call the API and save it for future queries. The storage will grow as it is used and API calls will shrink to minimum over time.

Technically it will be a storage instead of a cache because it will have no expiration date.
 
Back
Top