https://github.com/redis-developer/redis-caching-api-responses
https://github.com/redis-developer/redis-caching-api-responses
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/redis-developer/redis-caching-api-responses
- Owner: redis-developer
- Created: 2021-02-22T20:55:26.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-23T17:18:58.000Z (about 5 years ago)
- Last Synced: 2025-04-12T03:52:36.242Z (11 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 12
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Caching
Simple API caching example with Redis! This repository is based on the video [Caching API Responses with Redis](https://youtu.be/hRecenOBYlE). For further information on caching, check out these videos as well:
- [What is Caching?](https://youtu.be/NCnVw7ew9do)
- [Caching Slow Database Queries](https://youtu.be/Ph4W3pWA3eY)
## Prerequisites
- Node.js
- Redis
- axios
- ioredis
- [Open Weather Map API Key ](https://openweathermap.org/api)
## Installing this application
Clone the repository to your computer:
```bash
git clone https://github.com/redis-developer/redis-caching-api-responses
```
Install dependencies:
```bash
npm install
```
Add the Open Weather Map API Key to your Environment Variables:
```bash
export WEATHER_API_KEY=
```
## Database Preparation
Ensure you have Redis installed and running.
For Docker:
```bash
$ docker run -p 6379:6379 --name redis6 -d redis:6
```
### `api.js`
This file contains logic to demonstrate caching entries from a weather service API. You will need a free API key of your own to run this demonstration. You can get your own API key by following the [instructions at the Open WeatherMap API](https://openweathermap.org/api) site. The `getWeather()` function retrieves a JSON object containing real-time meteorological information on a given city. This example uses Oakland, but you can use whichever city you like.
Ensure Redis is running, then run the `api.js` file once. Since there is no cache entry, the code will retrieve the data from the Open Weather Map API.
```bash
$ node api.js
{
coord: { lon: -83.4, lat: 42.67 },
weather: [
{
id: 803,
main: 'Clouds',
description: 'broken clouds',
icon: '04d'
}
],
...
source: 'API',
responseTime: '180ms'
}
```
The code will have placed a copy of the entry in the Redis cache, so the next function call will return data from the cache. Note that the cached entry has a `TTL` (Time To Live) of one hour. Weather can change frequently, so an hour should provide a reasonable approximation of the current conditions. After an hour, the cache entry will be removed automatically, and a new cache entry will need to be stored. This ensures fresh, relevant data.
```bash
$ node api.js
{
coord: { lon: -83.4, lat: 42.67 },
weather: [
{
id: 803,
main: 'Clouds',
description: 'broken clouds',
icon: '04d'
}
],
...
source: 'cache',
responseTime: '9ms'
}
```