Caching
🔁 Caching the Response
You can cache the response by using the cache
option in httpGet
.
Pass a number to specify TTL in milliseconds, or true
for a permanent cache.
// Cache for 5 seconds
await httpGet('Main', '/endpoint', { cache: 5000 });
// Permanent cache
await httpGet('Main', '/endpoint', { cache: true });
🧹 Deleting the Cache
You can delete a cached entry in two ways:
1. Using the onRequest
hook
const hooks: ShapeRQHooks = {
onRequest: ({ url, cacheDel }) => {
cacheDel(url); // Delete cache for this URL before the request
},
};
await httpGet('Main', '/endpoint', { hooks });
2. Using cacheDel
directly
import { cacheDel } from 'shape-rq';
cacheDel('https://example.com');
📦 Retrieving from Cache
A repeated request to the same URL will return cached data automatically, as long as the TTL hasn't expired (or it's a permanent cache).
await httpGet('Main', '/asd', { hooks, cache: true });
// Later...
await httpGet('Main', '/asd', { hooks }); // ← Will return from cache
Last updated