Data freshness: Decide how long the cached data is valid. For example, if rankings don’t change frequently, you might cache them for several hours.
Cache keys: Use a unique cache key for each combination of parameters (e.g., year=2024&sort=points), so each variant can be cached independently.
Cache invalidation: Define when and how the cache should be invalidated or updated (e.g., after a certain period or when new data is inserted).
2. Choose a Caching Layer
In-memory cache: Use libraries like node-cache or lru-cache if your application can handle caching in memory. This approach is simple and fast, but data is lost on server restarts.
Distributed cache: Use Redis or Memcached to cache data outside of your app's memory, making the cache shareable across multiple server instances and persistent through restarts.
3. Cache Query Results
When a request comes in for /riders/rankings?year=2024&limit=5&sort=points, first check if the sorted and processed result is available in the cache.
If it’s available, return the paginated subset directly from the cache.
If not, fetch the data from the database, process it, sort it, and store the entire sorted result set in the cache. Return the paginated result for the initial request and subsequent requests.
4. Implementing Pagination with Cached Data
Store the full, sorted result set in the cache, allowing subsequent requests to apply pagination in memory rather than querying the database.
You can then return only the relevant portion (e.g., page 1, limit 5) based on the pagination parameters provided.
5. Example with in-memory cache
sing an in-memory cache in Node.js can be quite simple and is useful for caching data temporarily within the same server instance. Here’s how you can use an in-memory cache with the node-cache library, which is lightweight and provides basic caching functionalities like setting a TTL (time-to-live) for each cache item.
1. Install node-cache
npm install node-cache
2. Set Up the Cache
Initialize node-cache in your API, typically in a separate module so you can reuse it across various parts of your app.
stdTTL (default TTL) is set to 3600 seconds (1 hour), which means cached items expire after 1 hour.
checkperiod specifies how frequently (in seconds) expired items are removed from the cache.
3. Use the Cache in an API Route
Here’s an example of how you might use node-cache in a route that fetches rider rankings. It will check if the rankings are cached, and if not, it will query the database, cache the results, and return them.
// ridersController.js
const cache = require('./cache');
// Mock function that simulates an expensive database query
async function fetchAndCalculateRankingsFromDatabase(year, sort) {
// Simulate data retrieval and sorting...
return [
{ id: 1, name: 'Rider 1', points: 120 },
{ id: 2, name: 'Rider 2', points: 110 },
// Assume more data here...
];
}
async function getRiderRankings(req, res) {
const { year, limit = 5, page = 1, sort = 'points' } = req.query;
const cacheKey = `rankings:${year}:${sort}`;
// Check if rankings are in cache
let rankings = cache.get(cacheKey);
if (!rankings) {
// If not cached, fetch from database and process the data
rankings = await fetchAndCalculateRankingsFromDatabase(year, sort);
// Cache the processed data
cache.set(cacheKey, rankings);
}
// Paginate the cached data
const startIndex = (page - 1) * limit;
const endIndex = startIndex + limit;
const paginatedRankings = rankings.slice(startIndex, endIndex);
res.json(paginatedRankings);
}
Explanation
Check Cache: The function first looks in the cache for cacheKey (a key based on query parameters to ensure uniqueness). If it’s found, it uses the cached data directly.
Fetch Data If Not Cached: If not found in the cache, it fetches and processes the data from the database, caches it with cache.set(cacheKey, rankings), and then uses it.
Pagination: To support pagination, it slices the cached data according to page and limit parameters before returning the result.
Response: The API returns the paginated data, either from cache or after querying and caching it.
Additional Cache Management Methods
node-cache provides additional methods that can help manage your cache:
cache.get(key): Retrieves a cached item by key.
cache.set(key, value, ttl): Caches a value with an optional TTL (overrides the default).
cache.del(key): Deletes an item from the cache.
cache.flushAll(): Clears all cache items (useful if you need to invalidate all cached data at once).
cache.keys(): Lists all current cache keys.
Example for Clearing Cache on Data Updates
If rankings are recalculated or updated, you’ll want to clear the relevant cache. Here’s an example of how you might do this in an update function:
async function updateRiderRankings(newRankingsData) {
// Update rankings data in the database...
// Invalidate cache for any rankings that may be affected
cache.del(`rankings:2024:points`); // Adjust key as needed
}
1. Identify Caching Requirements
2. Choose a Caching Layer
3. Cache Query Results
4. Implementing Pagination with Cached Data
5. Example with in-memory cache
sing an in-memory cache in Node.js can be quite simple and is useful for caching data temporarily within the same server instance. Here’s how you can use an in-memory cache with the node-cache library, which is lightweight and provides basic caching functionalities like setting a TTL (time-to-live) for each cache item.
1. Install node-cache
npm install node-cache
2. Set Up the Cache
Initialize node-cache in your API, typically in a separate module so you can reuse it across various parts of your app.
In this setup:
stdTTL
(default TTL) is set to 3600 seconds (1 hour), which means cached items expire after 1 hour.checkperiod
specifies how frequently (in seconds) expired items are removed from the cache.3. Use the Cache in an API Route
Here’s an example of how you might use node-cache in a route that fetches rider rankings. It will check if the rankings are cached, and if not, it will query the database, cache the results, and return them.
Explanation
Additional Cache Management Methods node-cache provides additional methods that can help manage your cache:
Example for Clearing Cache on Data Updates
If rankings are recalculated or updated, you’ll want to clear the relevant cache. Here’s an example of how you might do this in an update function: