Define two lifetimes
A public catalog endpoint may admit twelve requests per tenant in a rolling minute and cache the catalog for fifteen seconds. The quota window governs admission. The cache TTL bounds how long a stored response can be reused.
Keep keys and decisions separate
Run the example as an ES module with flex-rate-limit@2.2.5 and cache-hub@2.2.5 in a fresh process. The limiter uses its default sliding window. Each admitted request counts towards the quota, including cache hits. The explicit delete represents invalidation after a successful catalog write. The direct cache read after deletion checks the miss without calling the request handler.
import assert from 'node:assert/strict';
import { RateLimiter } from 'flex-rate-limit';
import { MemoryCache } from 'cache-hub';
const limiter = new RateLimiter({ windowMs: 60_000, max: 12 });
const cache = new MemoryCache({ maxEntries: 100, defaultTtl: 15_000 });
const key = 'catalog:public:v1';
const tenantA = 'quota:tenant-a:catalog';
await cache.set(key, ['quarterly']);
for (let request = 1; request <= 12; request++) {
assert.equal((await limiter.check(tenantA)).allowed, true);
assert.deepEqual(await cache.get(key), ['quarterly']);
}
assert.equal(await cache.del(key), true);
assert.equal(await cache.get(key), undefined);
const next = await limiter.check(tenantA);
assert.equal(next.allowed, false);
assert.ok(next.retryAfter > 0);
assert.equal((await limiter.check('quota:tenant-b:catalog')).allowed, true);
console.log('12 cache hits, 1 cache miss after deletion, tenant A denied, tenant B allowed');
The local run observed all twelve hits, then a miss after deletion. Tenant A's thirteenth request was denied with a positive retry interval. Tenant B's first request was admitted.
Choose cross-instance behavior deliberately
After deletion, the next admitted request can reload the catalog from its source. TTL expiry can likewise cause a fresh read. Tenant A still has to wait for room in its quota window.
Memory stores isolate process state. Instances that share a quota need the same key scheme and a shared atomic backend such as RedisStore. If you use Response Cache Kit for HTTP snapshots, keep its application cache key partitioned by the request headers that select the representation and by user or tenant identity when appropriate. Keep cache invalidation tags separate from quota keys.