rednafi / redis-request-caching

🐍 Simple Python App to Demonstrate API Request Caching with Redis

Home Page:https://rednafi.github.io/digressions/python/database/2020/05/25/python-redis-cache.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redis || Request || Caching

🐍 Simple Python Application to Demonstrate API Request Caching with Redis

Description

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:

Requirements

Run the App

  • Clone the repository.

    git clone git@github.com:rednafi/redis-request-caching.git
    
  • Go to .env file and provide your MAPBOX_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:

    http://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 show false. 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 the cache attribute should show true.

  • You can also go to http://localhost:5000/docs and play around with the swagger UI.

    alt

  • The cached data can be monitored using redisinsight. Go to http://localhost:8001.

    Select the ADD REDIS DATABASE button and click add database.

    alt

    This should bring up a prompt like this:

    alt

    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:

    alt

    Select Browser button in the top left panel and there you'll be able to look at your cached responses.

    alt

Remarks

All the pieces of codes in the blog were written and tested with python 3.9 on a machine running Ubuntu 20.04.

Disclaimer

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.

About

🐍 Simple Python App to Demonstrate API Request Caching with Redis

https://rednafi.github.io/digressions/python/database/2020/05/25/python-redis-cache.html


Languages

Language:Python 69.0%Language:Makefile 19.5%Language:Dockerfile 6.4%Language:Shell 5.2%