Skip to content

Redis Cache

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 the Redis integration package:

Terminal window
npm install @xeno-js/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.

A typical configuration can be kept entirely in environment variables:

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_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'),
}
}))

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')

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)
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)

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.

Check:

  • REDIS_HOST
  • REDIS_PORT
  • REDIS_USERNAME
  • REDIS_PASSWORD
  • Redis availability

If the cache module receives neither an enabled in-memory configuration nor a cache provider, its configuration fails.


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