Cloud

Distributed Cache

TTL cache with tag-based invalidation and tenant isolation.

2 min readDocumentationEdit this page

Overview

The cloud cache provides an in-memory TTL cache with LRU eviction and tag-based invalidation. Keys can be prefixed by tenant ID for automatic data isolation.

Basic Usage

typescript
import { cache } from '@axiljs/cloud'
 
cache.set('user:42', { name: 'Tariq', role: 'admin' })
const user = cache.get('user:42')
 
cache.has('user:42') // true

Custom TTL and Tags

typescript
cache.set('report:monthly', reportData, {
  ttlMs: 300_000, // 5 minutes
  tags: ['tenant:acme-corp', 'reports'],
})

Tenant Isolation

Prefix keys with the tenant ID to ensure data isolation:

typescript
const tenantId = getTenantId(req)
const key = `tenant:${tenantId}:user:${userId}`
 
cache.set(key, userData, {
  tags: [`tenant:${tenantId}`],
})

Tag-Based Invalidation

Invalidate all cache entries matching a tag in one call:

typescript
const cleared = cache.invalidateTag('tenant:acme-corp')
console.log(`Cleared ${cleared} entries`)

Cache Maintenance

typescript
cache.prune()     // remove expired entries
cache.clear()     // remove everything
 
cache.stats()
// { size: 142, maxEntries: 10000, defaultTtlMs: 60000 }

Custom Instance

typescript
import { CloudCache } from '@axiljs/cloud'
 
const sessionCache = new CloudCache({
  defaultTtlMs: 1_800_000, // 30 minutes
  maxEntries: 50_000,
})

Options

OptionTypeDefaultDescription
defaultTtlMsnumber60000Default time-to-live in milliseconds
maxEntriesnumber10000Maximum entries before LRU eviction

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY