@garraflavatra/chowchow
v1.1.1
Published
Efficient and customisable caching made easy.
Downloads
8
Readme
Chowchow
Efficient and customisable server-side caching made easy. Chowchow caches everything you want to cache, from CMS data to API responses.
- 🚀 Makes your app faster.
- 💼 Bring your own function to get the data.
- 🕰 Expiration time from 2 seconds to 2 eons.
- 🪶 Extremely lightweight (1.8 kB).
Installation
npm install @garraflavatra/chowchow
Usage
import CacheStore from 'chowchow';
const cache = new CacheStore(
/* Store name */ 'cache',
/* Callback that fetches data */ async () => {
const res = await fetch('http://localhost:5000/myapi');
if (!res.ok) return { success: false };
const entries = await res.json();
const filtered = entries.filter(e => e.status === 'online');
if (!filtered.length) return { success: false };
return { success: true, data: onlineEntries };
},
/* Maximum age in minutes */ 15,
/* Cache file directory */ '.cache',
/* File name */ 'cache'
);
Get the data
If the cache is not expired yet according to the expiration time, it will return the cache. Otherwise it will fetch new data, save it to the store, and return it.
const data = cache.getData();
Read the cache
This returns the cache, regardless of whether it is expired or not.
const cachedData = cache.readCache();
Get fresh data
This fetches fresh data and returns it, regardless of whether it is expired or not. The function is the given callback parameter of the constructor. Note that this does not write the cache to the cache file.
const freshData = cache.getFreshData();