DEV Community

Cover image for Using Cache with JavaScript examples
Matheus Costa
Matheus Costa

Posted on • Updated on

Using Cache with JavaScript examples

What is cache and why do we need it?

In an application, caching is strategically storing data in memory somewhere to reduce latency or to avoid spending unnecessary resources. It can be in a server closer to the user or actually in the user computer to access specific data faster.

Trade-offs using Cache

As in cache you store items in memory for its fast access, it does not (naturally) have data persistency. In other words, if you reset the server you lose the data. Redis fix this issue by using memory dump to create a persistency snapshot.

By storing the data in memory, it is also more expensive due to the lower storage capacity than a HDD for example, so have a good strategy on what to store and for how long.

Cache Structure

Implementation wise, you only need to know that cache is used as a dictionary data structure, storing items with key/value pairs. For example, you can store data for certain http request by using its parameters as key.

Cache Example Use Case

Image a weather application using a 3rd party API for the weather data and this API charges you per request. Makes sense to return updated data for your user but is it critical enough to be accurate by each second? By having a cache with TTL of 60 seconds, no matter how many users are using your application in certain area, you would only make one request to the 3rd party API per minute while keeping your users updated with the cache data. This could reduce A LOT of requests to this API and save you money, while returning the data to the user way faster.

Simple in-memory cache example

For this example in Node.js we'll use the node-cache package to build an in-memory cache.

import axios from "axios";
import NodeCache from "node-cache";

class WeatherRepository {
  // Lets set the default TTL to 60 seconds, so the data will expire from the
  // cache in 60 seconds and we'll have to fetch the updated data from the API.

  weatherCache = new NodeCache({ stdTTL: 60 });

  async getCurrentWeatherInformation(city) {
    // First, lets check if the required data is in cache
    const cachedData = this.weatherCache.get(city);

    // If it is, return it
    if (cachedData) return cachedData;

    const { data: weatherData } = await axios.get(
      `https://weather-api-url/?city=${city}`
    );

    // Else, fetch the data from the API then save it on the cache.
    // Here we'll use the city as key.
    this.weatherCache.set(city, weatherData);

    return weatherData;
  }
}

export { WeatherRepository };
Enter fullscreen mode Exit fullscreen mode

If you need a remote data structure server with way more features than an in-memory cache, look for Redis.

Conclusion

Cache is a strategy extremely used for reducing latency, storing recent data or data that is likely to be reused. Hope you find this article useful and add cache to your programming arsenal. Let me know if you have any questions or suggestions!

Top comments (0)