UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
to your environment:
WEATHER_API_KEY
from Weather API Website for free and we will export it.
python-dotenv
to load environment variables from your .env
file.
main.py
:
http://127.0.0.1:8000/weather/istanbul
in your browser or use curl to get the weather data for Istanbul. The first request will fetch the data from the weather API and cache it, and subsequent requests will return the cached data until the cache expires after 10 minutes.
To monitor your data in Redis, you can use the Upstash Console and check out the Data Browser tab.
Redis.from_env()
to initialize the Redis connection using the environment variables. Redis will store the weather data with city names as cache keys.
/weather/{city}
endpoint, we check if the weather data is already cached by looking up the weather:{city}
key in Redis. If the data is found in cache, it’s returned immediately.
redis.setex()
, which stores the data with a 10-minute expiration.