axios-cache-lite

A zero-config, lightweight caching solution for Axios requests with TTL, stale-while-revalidate, and retry support.

Installation

npm install axios-cache-lite

# or

yarn add axios-cache-lite

Make sure you have axios installed as a peer dependency:

npm install axios

# or

yarn add axios

Features

🚀 Zero Config

Works out of the box with sensible defaults. No complex setup required.

💾 In-memory + localStorage

Dual-layer caching for optimal performance and persistence.

âąī¸ TTL Expiration

Set time-to-live for cache entries to keep data fresh.

🔄 Stale-While-Revalidate

Return stale data immediately while fetching fresh data in the background.

🔁 Automatic Retry

Built-in retry mechanism with exponential backoff for network errors.

đŸĒļ Lightweight

Core functionality in ~100 lines of code. No external dependencies.

Basic Usage

import { cachedAxios } from 'axios-cache-lite';

// Use like regular axios, but with caching
const response = await cachedAxios({
  url: 'https://api.example.com/data',
  method: 'GET',
  params: { id: 123 }
});

// With custom cache options
const response = await cachedAxios(
  {
    url: 'https://api.example.com/data',
    method: 'GET',
    params: { id: 123 }
  },
  {
    ttl: 300000, // 5 minutes in milliseconds
    staleWhileRevalidate: true,
    retry: 3,
    useLocalStorage: true
  }
);

Clearing Cache

import { clearCache } from 'axios-cache-lite';

// Clear a specific cache entry
clearCache('https://api.example.com/data{"id":123}');

// Clear all cache entries
clearCache();

Pro Features

For advanced use cases, axios-cache-lite offers Pro features:

import { cachedAxios } from 'axios-cache-lite';
import { enableProFeatures, getCacheInspector } from 'axios-cache-lite/pro';

// First, star our GitHub repo: https://github.com/Nom-nom-hub/axios-cache-lite

// Then enable pro features with your GitHub username
const enabled = await enableProFeatures({
  store: 'indexeddb', // 'indexeddb' or 'custom'
  strategy: 'LRU',    // 'LRU', 'LFU', or 'FIFO'
  licenseKey: '@yourusername', // Your GitHub username with @ prefix
  maxEntries: 1000,   // Maximum number of entries to keep in cache
  enableInspector: true // Enable cache inspector
});

How to Get Pro Features (Free!)

  1. Star our GitHub repository
  2. Use your GitHub username to enable Pro features

Benchmark

axios-cache-lite is designed for performance. Here are some benchmark results:

Operation Average Time
Direct axios (no cache) 100.00ms
First request (cache miss) 100.50ms
Second request (cache hit) 0.50ms
Stale-while-revalidate 0.60ms
Pro: IndexedDB + LRU (first) 105.00ms
Pro: IndexedDB + LRU (hit) 1.00ms
Cache hits are up to 200x faster! âšĄī¸

TypeScript Support

import { cachedAxios, CacheOptions } from 'axios-cache-lite';
import { enableProFeatures, ProFeatureOptions } from 'axios-cache-lite/pro';

const options: CacheOptions = {
  ttl: 60000,
  staleWhileRevalidate: true
};

const response = await cachedAxios<{ id: number, name: string }>(
  { url: 'https://api.example.com/data' },
  options
);

console.log(response.data.name); // TypeScript knows this is a string