axios-mongo-cache
v1.0.0
Published
Ultra-simple Axios caching interceptor with optional MongoDB persistence (auto-detects local Mongo on common ports).
Maintainers
Readme
axios-mongo-cache
Ultra-simple Axios caching interceptor with optional MongoDB persistence. It auto-detects local MongoDB on common ports, making it a breeze to set up.
Features
- Two-layer Cache: In-memory cache for speed, with an optional MongoDB backend for persistence.
- Auto-detection: Automatically discovers local MongoDB instances on common ports (27017-27020).
- Request Deduplication: Concurrent identical requests are deduplicated, with only one actual network request being made.
- Customizable: Configure cache TTL, which HTTP methods to cache, and more.
- Easy Setup: Integrates with Axios as an interceptor.
Installation
# Using npm
npm install axios-mongo-cache axios mongodb
# Using pnpm
pnpm add axios-mongo-cache axios mongodbUsage
Here's a quick example of how to use axios-mongo-cache:
import axios from 'axios';
import setupAxiosMongoCache from 'axios-mongo-cache';
async function run() {
// Create an Axios instance
const api = axios.create();
// Set up the cache
const { clearCache, close } = await setupAxiosMongoCache(api, {
dbName: 'MyCacheDB',
collectionName: 'NetworkRequestCache',
ttl: 1000 * 60 * 5, // 5 minutes
useMongo: true,
methods: ['get']
});
// Make a request - the first time it will hit the network
console.log('First fetch (network expected)');
const response1 = await api.get('https://jsonplaceholder.typicode.com/todos/1');
console.log('Data:', response1.data);
// Make the same request again - this time it will be served from the cache
console.log('\nSecond fetch (should be cached)');
const response2 = await api.get('https://jsonplaceholder.typicode.com/todos/1');
console.log('Data:', response2.data);
// Clean up
await clearCache();
await close();
}
run().catch(console.error);The setupAxiosMongoCache function returns two helper methods:
clearCache(): Clears both the in-memory and MongoDB cache.close(): Closes the MongoDB connection and ejects the interceptors.
Configuration
You can pass the following options to setupAxiosMongoCache:
| Option | Type | Default | Description |
| ---------------- | --------- | ------------------------- | ------------------------------------------------------------------------------------------------------- |
| mongoUri | string | null | Full MongoDB connection URI. If not provided, the library will attempt to auto-discover a local instance. |
| dbName | string | null | The name of the database to use. Required if useMongo is true and mongoUri is not provided. |
| collectionName | string | 'NetworkRequestCache' | The name of the collection to store cache data. |
| ttl | number | Infinity | Time-to-live for cache entries in milliseconds. Defaults to Infinity (never expire). |
| useMongo | boolean | true | Whether to use MongoDB for persistence. If false, only an in-memory cache is used. |
| methods | string[]| ['get'] | An array of HTTP methods to cache (in lowercase). |
License
This project is licensed under the MIT License - see the LICENSE file for details.
