Alokai

getOrSetCache

Add a record to Redis or retrieve it if already exists.

This method is a convenience method which combines the getCache() and setCache() methods. It first tries to retrieve the record and if it doesn't exist, it adds the record to Redis.

Signature

declare function getOrSetCache<TData = any>(
	props: SetCacheProps<TData>
): Promise<TData>;

Parameters

NameRequiredTypeDescription
propsRequiredSetCacheProps<TData>Parameter object which can be used with this method. Refer to its type definition to learn about possible properties.

Returns

If the record already exists, it returns the record data. Otherwise, it returns the data returned by the data callback.

Referenced Types

Examples

Getting a single record or setting it if it doesn't exist.

import { sdk } from '~/sdk';

const { id } = await sdk.redis.getOrSetCache({
  key: 'example-key',
  data: async () => ({ id: '1992695' }),
});

On this page