Skip to content

Caching Pipeline

Use the Caching Pipeline to cache the results of Queries in Xeno.JS.

The built-in caching behavior:

  1. checks whether the query has cache options;
  2. checks the cache when caching is not bypassed;
  3. returns the cached value when one is available;
  4. executes the query when there is no cached value;
  5. stores successful query results using the configured TTL.

Query result caching runs in the Query pipeline. When withQueryCaching() is enabled, Xeno.JS also adds a command pipeline behavior that invalidates cache entries declared by successful commands.

If you first need to create a Query, see Create a Query.

You need:

  • an XenoAppBuilder instance;
  • a cache, which Xeno.JS configures in-memory by default when query caching is enabled;
  • a Query;
  • a Query Handler;
  • the Query executed through the Xeno.JS mediator.

The cache can use the built-in in-memory implementation or Redis. Configure a cache explicitly only when you need to choose or customize its provider.

For the complete Query creation workflow, including how to extend Query and define its parameters, see Create a Query.

To use the default in-memory cache, configure it through XenoAppBuilder.addCache():

For example, to use the built-in in-memory cache:

import { XenoAppBuilder } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addCache()
.build()

The default XenoAppBuilder cache configuration uses the in-memory cache.

You can also explicitly set its in-memory option:

import { XenoAppBuilder } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addCache((options) => {
options.inMemory = true
})
.build()

To use Redis, pass the withRedis() plugin to addCache():

import { XenoAppBuilder } from '@xeno-js/core'
import { withRedis } from '@xeno-js/redis'
const app = new XenoAppBuilder()
.addCache(withRedis(() => {}))
.build()

See Redis Cache for Redis configuration options.

Enable query caching by passing withQueryCaching() to addPipeline():

import { XenoAppBuilder, withQueryCaching } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withQueryCaching())
.build()

This plugin enables the query-specific caching behavior. If no cache has already been registered, Xeno.JS automatically configures the default in-memory cache.

The two configuration steps have different responsibilities when you configure both:

  • addCache() configures or selects the cache implementation;
  • addPipeline(withQueryCaching()) enables query caching.

Calling addCache() is optional when the default in-memory cache is sufficient.

The caching pipeline reads caching information from the Query’s cacheOptions.

A Query can be created like this:

import { Query } from '@xeno-js/shared'
export class GetUserQuery extends Query<User> {
constructor(
public readonly userId: string,
) {
super('GetUserQuery', {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: false,
})
}
}

The Query itself is covered in detail in Create a Query.

That page should be your starting point when you need to:

  • create a Query class;
  • define Query parameters;
  • create a Query Handler;
  • connect the Query to its handler;
  • add validation to the Query.

This page focuses only on the caching options used by the Query.

Use cacheKey to identify the cached result.

For example:

super('GetUserQuery', {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: false,
})

The cache key should represent the input that determines the query result.

For example, a Query that retrieves a user by ID can use:

cacheKey: `user:${userId}`

A Query that retrieves a paginated list should include the parameters that affect the result:

cacheKey: `users:${page}:${pageSize}`

If the cacheKey is empty or missing, the caching behavior is skipped and the Query executes normally.

Use ttl to specify the lifetime of the cached result:

cacheOptions: {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: false,
}

The value is a TTL in seconds and is passed to the configured cache when the successful Query result is stored.

Choose a TTL according to how long the cached result can safely remain available for your application.

Set isUserScoped to true when the cached result must be isolated per user. The active identity must provide a userId; otherwise, the key builder falls back to the contextual key:

super('GetCurrentUserQuery', {
cacheKey: 'current-user',
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: true,
})

This is useful for Queries whose result depends on the current authenticated user.

For example:

export class GetCurrentUserQuery extends Query<User> {
constructor() {
super('GetCurrentUserQuery', {
cacheKey: 'current-user',
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: true,
})
}
}

Use user-scoped caching when the same logical Query can return different data for different users. The key builder includes the current user ID when available and applies the tenant context as well.

Set bypassCache to true when you want to skip reading an existing cached value:

super('GetUserQuery', {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: true,
consistentRead: false,
isUserScoped: false,
})

When bypassCache is enabled:

  1. Xeno.JS does not read the cached value;
  2. the Query executes normally;
  3. if the Query succeeds, its result is still stored in the cache.

For example:

Query
│
▼
bypassCache = true
│
├── skip cache read
│
▼
Query Handler
│
▼
successful result
│
▼
store result in cache

This skips the cached value and executes the Query handler; it does not change the consistency guarantees of the underlying data source. A successful result is still written to the cache.

Set consistentRead to true to skip the cached value and execute the Query handler. This option bypasses the cache; it does not change the consistency guarantees of the underlying data source:

super('GetUserQuery', {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: false,
consistentRead: true,
isUserScoped: false,
})

When consistentRead is true, Xeno.JS skips the cache read.

As with bypassCache, a successful result is still written to the cache.

Therefore:

consistentRead = true
│
▼
skip cache read
│
▼
execute Query
│
▼
successful result
│
▼
update cache

When caching is enabled and a cached value exists for the Query’s cache key, Xeno.JS returns that value immediately.

The Query Handler is not executed.

For example:

Query
│
▼
Cache lookup
│
├── HIT ──► return cached result
│
└── MISS
│
▼
Query Handler

This means the handler is not called again when a valid cached result is available.

When there is no cached value, Xeno.JS executes the Query normally.

If the Query succeeds, the result is stored in the cache using the configured cacheKey and ttl.

For example:

Query
│
▼
Cache lookup
│
└── MISS
│
▼
Query Handler
│
▼
successful result
│
▼
store in cache
│
▼
return result

The same Query can then use the cached result on subsequent executions.

No.

The caching pipeline stores the result only when the Query returns a successful Result.

If the Query fails:

return Result.fail(error)

the result is returned to the caller but is not written to the cache.

This prevents a failed execution from becoming the cached response for subsequent requests.

A cache read failure does not fail the Query.

For example, if the cache service is temporarily unavailable:

Query
│
▼
Cache read
│
└── error
│
▼
log warning
│
▼
execute Query

Xeno.JS logs a warning and continues with normal Query execution.

This allows the application to continue serving the Query even when the cache is unavailable.

A cache write failure also does not fail an otherwise successful Query.

The Query result is returned normally and Xeno.JS logs a warning about the cache error.

For example:

Query Handler
│
▼
successful result
│
├── cache write succeeds ──► return result
│
└── cache write fails
│
▼
log warning
│
▼
return result

The cache is therefore an optimization for Query execution rather than a requirement for returning a successful Query result.

The following example shows the relevant application configuration and a cacheable Query.

import { XenoAppBuilder, withQueryCaching } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addCache()
.addPipeline(withQueryCaching())
.build()
import { Query } from '@xeno-js/shared'
export class GetUserQuery extends Query<User> {
constructor(
public readonly userId: string,
) {
super('GetUserQuery', {
cacheKey: `user:${userId}`,
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: false,
})
}
}

Execute the Query through the mediator as you normally would:

const result = await mediator.query(
new GetUserQuery(userId),
signal,
)

The first successful execution populates the cache.

Subsequent executions using the same cache key can return the cached result without executing the Query Handler.

For the complete Query implementation and Handler registration workflow, see Create a Query and Create a Query Handler.

Check:

  1. withQueryCaching() is passed to addPipeline();
  2. the Query defines a non-empty cacheKey;
  3. the Query is executed through the Xeno.JS mediator.

For example:

import { XenoAppBuilder, withQueryCaching } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withQueryCaching())
.build()

Check:

  1. the Query has a cacheKey;
  2. the cache key is stable for the same Query parameters;
  3. bypassCache is not true;
  4. consistentRead is not true;
  5. the configured TTL has not expired.

Also make sure that the cache key includes every parameter that can change the Query result.

Different users receive the same cached result

Section titled “Different users receive the same cached result”

If the Query result depends on the current user, use:

isUserScoped: true

The current identity must contain a distinct userId for each user. Without it, the key builder falls back to the contextual key.

For example:

super('GetCurrentUserQuery', {
cacheKey: 'current-user',
ttl: 60,
bypassCache: false,
consistentRead: false,
isUserScoped: true,
})

The application fails while enabling query caching

Section titled “The application fails while enabling query caching”

Make sure withQueryCaching() is passed to addPipeline(). If no cache is already registered, Xeno.JS configures the default in-memory cache automatically.

For example:

import { XenoAppBuilder, withQueryCaching } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withQueryCaching())
.build()

Use addCache() only when you need to select or customize a cache provider.

Cache read and write failures are logged as warnings.

The Query itself should continue executing when a cache operation fails.

Check the cache configuration if these warnings persist, especially when using Redis.


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