Redis Cache
Introduction
Section titled “Introduction”Xeno.JS can use Redis as the application’s cache backend through the withRedis cache plugin.
Pass the plugin to XenoAppBuilder.addCache(). The plugin supplies a Redis cache provider to the cache module.
Install Redis Support
Section titled “Install Redis Support”Install the Redis integration package:
npm install @xeno-js/redisConfigure Redis
Section titled “Configure Redis”Pass withRedis to addCache(). Its setup callback receives Redis configuration; the plugin defaults to enabled, with host localhost and port 6379.
import { XenoAppBuilder } from '@xeno-js/core'import { withRedis } from '@xeno-js/redis'
const app = new XenoAppBuilder()
app.addCache(withRedis((options, config) => { options.host = config.get('REDIS_HOST') ?? 'localhost' options.port = config.getNumber('REDIS_PORT') ?? 6379 options.options = { username: config.get('REDIS_USERNAME'), password: config.get('REDIS_PASSWORD'), }}))The Redis configuration fields shown by the plugin are:
| Field | Description |
|---|---|
isEnabled |
Enables the Redis provider when true; defaults to true. |
host |
Redis server hostname; defaults to localhost. |
port |
Redis server port; defaults to 6379. |
options |
Options object initialized with username and password from configuration. |
Using Environment Variables
Section titled “Using Environment Variables”A typical configuration can be kept entirely in environment variables:
REDIS_HOST=localhostREDIS_PORT=6379REDIS_USERNAME=REDIS_PASSWORD=Then configure the application:
import { withRedis } from '@xeno-js/redis'
app.addCache(withRedis((options, config) => { options.host = config.get('REDIS_HOST') ?? 'localhost' options.port = config.getNumber('REDIS_PORT') ?? 6379 options.options = { username: config.get('REDIS_USERNAME'), password: config.get('REDIS_PASSWORD'), }}))Resolve the Redis Cache
Section titled “Resolve the Redis Cache”The application does not resolve Redis directly.
Resolve the common Xeno.JS cache token:
import { XenoAppBuilder, TOKENS } from '@xeno-js/core'import { withRedis } from '@xeno-js/redis'
const app = new XenoAppBuilder()
app.addCache(withRedis(() => {}))
await app.build()
const cache = app.resolve(TOKENS.CACHE)TOKENS.CACHE returns the configured Redis-backed cache.
Your application therefore remains independent from the Redis client:
await cache.set('user:123', { id: '123' }, 60)
const user = await cache.get<{ id: string }>('user:123')Redis Cache Operations
Section titled “Redis Cache Operations”The Redis-backed cache supports the same cache API described in the Cache Overview:
await cache.set('key', value, 60)
const value = await cache.get<MyValue>('key')
await cache.remove('key')
const exists = await cache.has('key')
await cache.clear()It also supports atomic operations used by Xeno.JS features:
await cache.setIfAbsent('lock:key', true, 30)
await cache.increment('counter', 60)Complete Example
Section titled “Complete Example”import { XenoAppBuilder, TOKENS } from '@xeno-js/core'import { withRedis } from '@xeno-js/redis'
const app = new XenoAppBuilder()
app.addCache(withRedis((options, config) => { options.host = config.get('REDIS_HOST') ?? 'localhost' options.port = config.getNumber('REDIS_PORT') ?? 6379 options.options = { username: config.get('REDIS_USERNAME'), password: config.get('REDIS_PASSWORD'), }}))
await app.build()
const cache = app.resolve(TOKENS.CACHE)
await cache.set('example', { value: 'hello' }, 60)
const value = await cache.get<{ value: string }>('example')
console.log(value)Troubleshooting
Section titled “Troubleshooting”Redis Is Not Being Used
Section titled “Redis Is Not Being Used”Pass the Redis cache plugin to addCache():
app.addCache(withRedis(() => {}))The plugin selects its Redis provider by default. Set isEnabled to false in its setup callback to use the in-memory cache instead.
Redis Connection Fails
Section titled “Redis Connection Fails”Check:
REDIS_HOSTREDIS_PORTREDIS_USERNAMEREDIS_PASSWORD- Redis availability
The Cache Is Not Registered
Section titled “The Cache Is Not Registered”If the cache module receives neither an enabled in-memory configuration nor a cache provider, its configuration fails.
Related Documentation
Section titled “Related Documentation”Support Us
Section titled “Support Us”Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section
