Skip to main content
UpstashRedisCache is the Cache component’s serverless backend. It talks to Upstash Redis over its HTTP REST API, with no persistent TCP connection, which makes it a natural fit for edge and serverless runtimes. It implements the same ICache interface as every other backend, so you can develop against the filesystem or a local Redis and switch to Upstash in production without changing a single call site.

What this backend offers

The HTTP REST transport means no TCP pooling, so it runs in edge functions and short-lived runtimes where pooling is awkward. The interface is the same ICache you use elsewhere: get, set, delete, has, deleteByPrefix, and clear. Every key is prefixed with a namespace (default cache), which lets several apps share one Upstash database without colliding. The bulk operations, deleteByPrefix() and clear(), use cursor-based SCAN with pipelined deletes so they stay efficient on large keyspaces. The class registers with @decorator.cache() and resolves from the container.

Installation

UpstashRedisCache ships with @talosjs/cache and depends on the Upstash Redis client.

Environment variables

Both can also be passed through constructor options (url, token), which take precedence over the environment. Either way they are validated when the cache is constructed, so misconfiguration fails fast.

Options

UpstashRedisCache accepts an options object as its second constructor argument:

How it works

Keys are namespaced before they hit Redis: set("user:1", …) with the default namespace writes cache:user:1. Reads, writes, and existence checks map straight onto Upstash commands, while the bulk operations iterate the keyspace with SCAN and delete matches in a pipeline.

Usage

The API is identical to every other cache backend.
To run several isolated caches against one Upstash database, give each its own namespace:

Use in the app

In an @talosjs/app application, pass UpstashRedisCache to the cache slot of your App config. The framework registers it and exposes it as the "cache" container constant.
With a cache configured, any route that declares a cache key has its response cached automatically. The framework builds the cache key from the route’s cache prefix, HTTP method, URL, and the authenticated user’s id:
You can also reach the same instance anywhere for manual caching, either by injecting it or by reading the constant:
Set CACHE_UPSTASH_REDIS_REST_URL and CACHE_UPSTASH_REDIS_REST_TOKEN in your .env.yml (or environment) so the cache can connect.

Exceptions

UpstashRedisCache throws CacheException on misconfiguration, carrying a machine-readable key.

Caching without surprises

Pass ttl (seconds) on set() for anything volatile so transient values expire instead of piling up. Give each concern its own namespace (sessions, rate counters, query cache) so clear() and deleteByPrefix() only touch what you mean them to. Group related keys with structured prefixes like user:123:profile, which lets deleteByPrefix("user:123:") invalidate them together. Load the REST URL and token from .env, never from source. And since the ICache calls stay identical, develop against the filesystem or a local Redis and switch to Upstash for production. See the Cache component for the full interface and the other backends.