Caching API Requests

When making requests to an external service’s API, some requests will frequently occur with the same parameters and return the same result. If we cache our request or response, we can reduce HTTP requests, which can improve performance and avoid hitting rate limits.

The APICache Ruby gem is a good choice for caching the API responses (typically JSON) in any Moneta store, such as Memcache or Redis.

However, we don’t always need to cache the entire API response. We can save space, avoid adding the operational overhead of Memcache or Redis, and avoid repeating the JSON parsing step if we cache only the URL requested.

Foursquare venue search

In the following example, our app only needs a venue’s name, latitude, longitude, and street address. We’ll get the data from Foursquare’s venue search API by category (“restaurant”) and neighborhood (“The Mission”).

What’s next

Hound
automatically reviews Ruby, JavaScript, CoffeeScript, and SCSS code
in your GitHub pull requests and comments on style violations.
It is free for open source repos and $12/month per private repo.