๐ Simple Python Application to Demonstrate API Request Caching with Redis
This app sends request to Mapbox's route optimization API and caches the return value in a Redis database for 1 hours. Meanwhile, if new a new request arrives, the app first checks if the return value exists in the Redis cache. If the value exists, it shows the cached value, otherwise, it sends a new request to the Mapbox API, cache that value and then shows the result.
The app uses the following stack:
- Httpx for sending the requests
- Redis for caching
- RedisInsight for monitoring the caches
- FastAPI for wrapping the original API and exposing a new one
- Docker and docker-compose for deployment
-
Clone the repository.
git clone [email protected]:rednafi/redis-request-caching.git
-
Go to
.env
file and provide yourMAPBOX_ACCESS_TOKEN
. You can get it from here.MAPBOX_ACCESS_TOKEN="Your-Mapbox-API-token"
-
In the
.env
file, replace the host ip with your own local ip -
Go to the root directory and run:
docker-compose up -d
-
Go to your browser and hit the following url:
https://localhost:5000/route-optima/90.3866,23.7182;90.3742,23.7461
-
This should return a response like the following:
{ "code":"Ok", "waypoints":[ { "distance":26.041809241776583, "name":"", "location":[ 90.386855, 23.718213 ], "waypoint_index":0, "trips_index":0 }, { "distance":6.286653078791968, "name":"", "location":[ 90.374253, 23.746129 ], "waypoint_index":1, "trips_index":0 } ], "trips":[ { "geometry":{ "coordinates":[ [ 90.386855, 23.718213 ], "... ..." ], "type":"LineString" }, "legs":[ { "summary":"", "weight":3303.1, "duration":2842.8, "steps":[ ], "distance":5250.2 }, { "summary":"", "weight":2536.5, "duration":2297, "steps":[ ], "distance":4554.8 } ], "weight_name":"routability", "weight":5839.6, "duration":5139.8, "distance":9805 } ], "cache":false }
-
If you've hit the above url for the first time, the
cache
attribute of the json response should showfalse
. This means that the response is being served from the original MapBox API. However, hitting the same url with the same coordinates again will show the cached response and this time thecache
attribute should showtrue
. -
You can also go to
https://localhost:5000/docs
and play around with the swagger UI. -
The cached data can be monitored using redisinsight. Go to
https://localhost:8001
.Select the
ADD REDIS DATABASE
button and clickadd database
.This should bring up a prompt like this:
Give your database a name and provide the localhost IP adress in the form. Keep the username field black and use
ubuntu
as the password.Once you've done that you'll be taken to a page like the following:
Select
Browser
button in the top left panel and there you'll be able to look at your cached responses.
All the pieces of codes in the blog were written and tested with python 3.9 on a machine running Ubuntu 20.04.
This app has been made for demonstration purpose only. So it might not reflect the best practices of production ready applications. Using APIs without authentication like this is not recommended.