🎉 Version 1.8.0 is out! Use shared cache for the Next.js Pages API routes and getServerSideProps with neshClassicCache
Built-in Handlersredis-strings

redis-strings Handler

import { createClient } from 'redis';
import createRedisHandler from '@neshca/cache-handler/redis-strings';
 
const client = createClient(clientOptions);
 
await client.connect();
 
const redisHandler = createRedisHandler({
  client,
  keyPrefix: 'prefix:',
  timeoutMs: 1000,
  keyExpirationStrategy: 'EXAT',
  sharedTagsKey: '__sharedTags__',
  revalidateTagQuerySize: 100,
});

The redis-strings Handler uses plain Redis as the cache store. It is a simple and fast cache handler that is suitable for most applications. This Handler is ideal for applications that require fast and efficient cache management without the need for advanced features like full-text search for custom revalidation strategies.

API

@neshca/cache-handler/redis-strings exports a function that creates a new Handler instance for the redis-strings Handler.

Parameters

  • options - An object containing the following properties:
    • client - A Redis client instance. The client must be ready before creating the Handler.
    • keyPrefix - Optional. Prefix for all keys, useful for namespacing. Defaults to an empty string.
    • timeoutMs - Optional. Timeout in milliseconds for Redis operations. Defaults to 5000. For disabling timeouts, set it to 0.
    • keyExpirationStrategy - Optional. It allows you to choose the expiration strategy for cache keys. Defaults to EXPRIREAT.
    • sharedTagsKey - Optional. Dedicated key for the internal revalidation process. It must not interfere with cache keys from your application. Defaults to __sharedTags__.
    • revalidateTagQuerySize - Optional. The number of tags in a single query retrieved from Redis when scanning or searching for tags. Defaults to 100.

keyExpirationStrategy

  • 'EXAT': Uses the EXAT option of the SET command to set the expiration time. This is more efficient than EXPIREAT. Requires Redis server 6.2.0 or newer
  • 'EXPIREAT': Uses the EXPIREAT command to set the expiration time. This requires an additional command call. Requires Redis server 4.0.0 or newer.

revalidateTagQuerySize

You can adjust this value to optimize the number of commands sent to Redis when scanning or searching for tags. A higher value will reduce the number of commands sent to Redis, but it will also increase the amount of data transferred over the network. Redis uses TCP and typically has 65,535 bytes as the maximum size of a packet (it can be lower depending on MTU).

Return value

A new Handler instance for the redis-strings Handler.