Skip to content
Back to skills

Stacks Cache

ASecurity

Use when implementing caching in Stacks - memory cache, Redis cache, cache-aside pattern (getOrSet), TTL management, cache stats, or cache configuration. Covers @stacksjs/cache and config/cache.ts.

  • 631 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added June 5, 2026
developmenttypescriptgobashapidatabase

Works with

  • api

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add stacksjs/stacks --skill stacks-cache --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Stacks Cache?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Stacks Cache
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/stacksjs-stacks-cache/badge)](https://www.skillsdirectory.com/skills/stacksjs-stacks-cache)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: stacks-cache
description: Use when implementing caching in Stacks - memory cache, Redis cache, cache-aside pattern (getOrSet), TTL management, cache stats, or cache configuration. Covers @stacksjs/cache and config/cache.ts.
license: MIT
compatibility: Bun >= 1.3.0, TypeScript
allowed-tools: Read Edit Write Bash Grep Glob
---

# Stacks Cache

## Key Paths
- Core package: `storage/framework/core/cache/src/`
- Configuration: `config/cache.ts`
- Cache storage: `storage/framework/cache/`

## Cache API (StacksCache)

```typescript
import { cache, memory } from '@stacksjs/cache'

// Get/Set
await cache.get<string>('key')                    // T | undefined
await cache.set('key', 'value', 3600)             // TTL in seconds
await cache.setForever('key', 'value')            // no expiry
await cache.mget<string>(['key1', 'key2'])        // T[]
await cache.mset([{ key: 'k1', value: 'v1', ttl: 60 }, ...])

// Cache-aside pattern
const value = await cache.getOrSet('expensive-key', async () => {
  return await computeExpensiveValue()
}, 3600)

// Delete
await cache.del('key')
await cache.del(['key1', 'key2'])
await cache.deleteMany(['key1', 'key2'])
await cache.remove('key')

// Existence
await cache.has('key')          // boolean
await cache.missing('key')     // boolean

// Take (get and delete)
const value = await cache.take<string>('key')

// TTL management
await cache.getTtl('key')              // remaining TTL
await cache.ttl('key', 7200)           // update TTL

// Clear
await cache.clear()
await cache.flush()

// Stats
const stats = await cache.getStats()
// { hits, misses, keys, size, hitRate }

// Keys
const keys = await cache.keys('user:*')  // pattern matching

// Cleanup
await cache.close()
await cache.disconnect()
```

## Factory Functions

```typescript
import { createMemoryCache, createRedisCache, createCache } from '@stacksjs/cache'

const memCache = createMemoryCache({
  stdTTL: 3600,       // default TTL (seconds)
  checkPeriod: 120,   // eviction check interval
  maxKeys: -1,        // -1 = unlimited
  useClones: true,    // clone on get/set
  prefix: 'app:'      // key prefix
})

const redisCache = createRedisCache({
  url: 'redis://localhost:6379',
  // or individual:
  host: 'localhost',
  port: 6379,
  username: undefined,
  password: undefined,
  database: 0,
  tls: false,
  stdTTL: 3600,
  prefix: 'app:'
})

// Auto-detect from config
const cache = createCache('memory')
const cache = createCache('redis', { host: 'localhost' })
```

## config/cache.ts
```typescript
{
  driver: 'memory',     // 'memory' | 'redis'
  prefix: 'stacks',
  ttl: 3600,            // 1 hour default
  maxKeys: -1,          // unlimited
  useClones: true,
  drivers: {
    redis: { host: 'localhost', port: 6379 },
    memory: {}
  }
}
```

## Default Instance
- `cache` — default memory cache (pre-configured)
- `memory` — alias for `cache`

## Gotchas
- Default driver is `memory` — data lost on restart
- Redis driver requires a running Redis server
- `getOrSet()` is the cache-aside pattern — fetches only on miss
- `useClones: true` means mutations to retrieved objects don't affect cache
- `take()` atomically gets and deletes — useful for one-time tokens
- Cache stats track hits, misses, and hit rate — useful for tuning
- `keys()` supports glob patterns for key listing
- Framework caches (auto-imports, discovered packages) are in `storage/framework/cache/`
- Use `buddy clean` or `buddy fresh` to clear framework caches

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.