Guide 2.23: Developer Guide

Caching SdashAPI Responses

Past exam questions are historical facts—they never change! Once you fetch the JAMB 2014 Chemistry questions, they will remain exactly the same forever. Because this data is immutable, SdashAPI is a perfect candidate for aggressive caching.

Why Cache?

2.23.1

Caching saves you money by drastically reducing the number of requests you make to our servers (protecting your monthly quota). It also makes your application feel incredibly fast to the user, as cached questions can be loaded instantly from their device's memory without waiting for a network round-trip.

Client-Side Caching with LocalStorage

2.23.2

If you are building a web application, you can easily save fetched batches of questions into the browser's localStorage or sessionStorage.

// Before making an API call, check if we already have the data
const cachedData = localStorage.getItem('jamb_physics_2019');

if (cachedData) {
  // Use the instantly available local data
  renderQuiz(JSON.parse(cachedData));
} else {
  // Otherwise, fetch from SdashAPI
  fetch('https://sdashapi.com/api/v1/q?subject=physics&year=2019&limit=50', { headers })
    .then(res => res.json())
    .then(json => {
      // Save it to localStorage for next time!
      localStorage.setItem('jamb_physics_2019', JSON.stringify(json.data));
      renderQuiz(json.data);
    });
}

Server-Side Caching with Redis

2.23.3

If you are proxying requests through your own Node.js or PHP backend, you should implement an in-memory cache like Redis. When User A requests WASSCE Mathematics questions, your backend fetches them from SdashAPI and saves them to Redis. When User B requests the exact same test 10 minutes later, your backend serves the questions instantly from Redis without consuming any SdashAPI credits.

You've mastered the API!

Next, we will show you how to build complete applications using these exact concepts.

Next Guide: Build a CBT App →
Avatar

How can we help?

We reply immediately

Hello! 👋 How can we help you today?