Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Plugins Ecosystem/Redis Integration

Redis Integration

Distinguish the memory default from a real Redis client.

Default behavior

redisPlugin() creates DefaultMemoryRedisClient. Supplying url alone does not open a Redis connection in the current implementation; prefix is not applied to keys. This runnable example uses the memory store explicitly:

index.ts
import { Volt, redisPlugin, type VoltRedisClient } from 'voltts';

const app = new Volt();
// Without an injected client this is an in-memory implementation.
app.plugin(redisPlugin());
app.get('/stats', async (c) => {
  const redis: VoltRedisClient = c.store.redis;
  const cached = await redis.get('stats');
  if (cached) return c.json({ source: 'cache', stats: JSON.parse(cached) });
  const stats = { products: 10 };
  await redis.set('stats', JSON.stringify(stats), 30);
  return c.json({ source: 'computed', stats });
});
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Verify a cache hit

bash
bun run index.ts
curl http://localhost:3000/stats
curl http://localhost:3000/stats

The second response reads the memory cache. Values expire after 30 seconds. The interface uses set(key, value, ttlSeconds), not setex.

Connect a real client

Initialize your Redis driver before plugin registration and pass a compatible client through redisPlugin({ client }). It must provide get, set with an optional TTL in seconds, del, has, and optional close. Driver method names and TTL signatures differ, so adapt them to VoltRedisClient explicitly. The plugin does not create a connection pool, retry driver connections, or provide decorator caching. See Response Caching for HTTP response caching.

Complete real Redis adapter

This recipe requires a running Redis instance. Install the driver and configure its URL:

bash
bun add redis@5
export REDIS_URL='redis://localhost:6379'
index.ts
import { Volt, redisPlugin, type VoltRedisClient } from 'voltts';
import { createClient } from 'redis';

const raw = createClient({ url: process.env.REDIS_URL });
raw.on('error', (error) => console.error('Redis connection error', error));
await raw.connect();
const client: VoltRedisClient = {
  get: (key) => raw.get(key),
  async set(key, value, ttlSeconds) {
    if (ttlSeconds !== undefined) await raw.set(key, value, { EX: ttlSeconds });
    else await raw.set(key, value);
  },
  async del(key) { return (await raw.del(key)) > 0; },
  async has(key) { return (await raw.exists(key)) > 0; },
  async close() { await raw.close(); },
};
const app = new Volt();
app.plugin(redisPlugin({ client }));
app.get('/redis-check', async () => {
  await client.set('volt:check', 'connected', 30);
  return { value: await client.get('volt:check') };
});
process.once('SIGTERM', async () => { await app.stop(); process.exit(0); });
app.listen(3000);
bash
bun run index.ts
curl http://localhost:3000/redis-check

Expect {"value":"connected"}. The adapter maps the driver's TTL and connection lifecycle to Volt's interface. Prefix your keys explicitly. This recipe follows the node-redis connection guide and requires integration testing against your service.