UNPKG

bentocache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

215 lines (211 loc) 5.63 kB
import { Knex } from 'knex'; import { Kysely } from 'kysely'; import { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'; import { Redis, RedisOptions } from 'ioredis'; import { DbResult, DefaultColumnTypes, DefaultSchemaConfig } from 'orchid-orm'; /** * A Duration can be a number in milliseconds or a string formatted as a duration * * Formats accepted are : * - Simple number in milliseconds * - String formatted as a duration. Uses https://github.com/lukeed/ms under the hood */ type Duration = number | string | null; /** * A Factory is basically just a function that returns a value */ type Factory<T = any> = T | (() => T) | Promise<T> | (() => Promise<T>); type GetSetFactoryContext = { /** * Dynamically set the TTL * @see https://bentocache.dev/docs/adaptive-caching * * @deprecated use `setOptions` instead */ setTtl: (ttl: Duration) => void; /** * Set the options for the current factory */ setOptions: (options: { ttl?: Duration; skipBusNotify?: boolean; skipL2Write?: boolean; }) => void; /** * Set the tags for the current factory */ setTags: (tags: string[]) => void; /** * Make the factory fail with a custom error. * Nothing will be cached and if a graced value is available, it will be returned */ fail: (message?: string) => void; /** * Make the factory do not cache anything. **If a graced value is available, * it will not be used** */ skip: () => undefined; /** * Graced entry if available */ gracedEntry: { value: any; } | undefined; }; /** * GetOrSet Factory */ type GetSetFactory<T = any> = (options: GetSetFactoryContext) => T | Promise<T>; /** * Options that are common to all drivers * * Some of theses options may be also defined in * the BentoCache options. Setting them specifically * for a driver will override the BentoCache options. */ type DriverCommonOptions = { prefix?: string; }; /** * Options for DynamoDB driver */ type DynamoDBConfig = { /** * DynamoDB table name to use. */ table: { name: string; }; /** * AWS credentials */ credentials?: DynamoDBClientConfig['credentials']; /** * Region of your DynamoDB instance */ region: DynamoDBClientConfig['region']; /** * Endpoint to your DynamoDB instance */ endpoint: DynamoDBClientConfig['endpoint']; } & DriverCommonOptions; /** * A number of bytes * Can be represented as a number or a string * e.g. '1kb', '1mb', '1gb' * We use https://www.npmjs.com/package/bytes under the hood */ type Bytes = number | string; /** * Options for Memory driver */ type MemoryConfig = { /** * Maximum number of items to store in the cache. * * Note that fewer items may be stored if you * are also using `maxSize` and the cache is full. * * @default 1000 */ maxItems?: number; /** * Maximum size of the cache in bytes. */ maxSize?: Bytes; /** * Maximum size of one entry in bytes. * * If an entry is larger than this value, * it will NOT be stored */ maxEntrySize?: Bytes; /** * Should the entries be serialized before storing * them in the cache. * * Note that, if unset, you cannot use maxSize or maxEntrySize * since the size of deserialized objects cannot be calculated. * * **Also make sure to read the below documentation. This option * can cause issues if not used correctly.** * * @see http://bentocache.dev/docs/cache-drivers#serialize-option * @default true */ serialize?: boolean; } & DriverCommonOptions; /** * Options for Redis driver */ type RedisConfig = { /** * A IoRedis connection instance or connection options */ connection: Redis | RedisOptions; } & DriverCommonOptions; /** * Options for File driver */ type FileConfig = { /** * Directory where the cache files will be stored */ directory: string; /** * The interval between each expired entry pruning * Can be set to `false` to disable pruning. * * @default false */ pruneInterval?: Duration | false; } & DriverCommonOptions; /** * Common options for database drivers */ interface DatabaseConfig extends DriverCommonOptions { /** * Table name to use */ tableName?: string; /** * Should the driver automatically create the table * @default true */ autoCreateTable?: boolean; /** * The interval between each expired entry pruning * run. Can be set to `false` to disable pruning. * * @default false */ pruneInterval?: Duration | false; } /** * Configuration accepted by the Knex adapter */ interface KnexConfig extends DatabaseConfig { /** * The Knex instance */ connection: Knex; } /** * Configuration accepted by the Kysely adapter */ interface KyselyConfig extends DatabaseConfig { /** * The Kysely instance */ connection: Kysely<any>; } /** * Configuration accepted by the Orchid ORM adapter */ interface OrchidConfig extends DatabaseConfig { /** * The Orchid ORM instance */ connection: DbResult<DefaultColumnTypes<DefaultSchemaConfig>>; } export type { Bytes as B, DynamoDBConfig as D, FileConfig as F, GetSetFactory as G, KnexConfig as K, MemoryConfig as M, OrchidConfig as O, RedisConfig as R, KyselyConfig as a, DatabaseConfig as b, DriverCommonOptions as c, Duration as d, Factory as e, GetSetFactoryContext as f };