UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

106 lines (105 loc) 4.3 kB
/** * @module Cache */ import { type StandardSchemaV1 } from "@standard-schema/spec"; import { type CacheAdapterVariants, type ICache, type ICacheFactory } from "../../../../cache/contracts/_module.js"; import { type CacheSettingsBase } from "../../../../cache/implementations/derivables/cache/_module.js"; import { type IEventBus } from "../../../../event-bus/contracts/_module.js"; import { type Namespace } from "../../../../namespace/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/cache"` * @group Derivables */ export type CacheAdapters<TAdapters extends string = string> = Partial<Record<TAdapters, CacheAdapterVariants<any>>>; /** * * IMPORT_PATH: `"@daiso-tech/core/cache"` * @group Derivables */ export type CacheFactorySettings<TAdapters extends string = string, TType = unknown> = CacheSettingsBase<TType> & { adapters: CacheAdapters<TAdapters>; defaultAdapter?: NoInfer<TAdapters>; }; /** * The `CacheFactory` class is immutable. * * IMPORT_PATH: `"@daiso-tech/core/cache"` * @group Derivables */ export declare class CacheFactory<TAdapters extends string = string, TType = unknown> implements ICacheFactory<TAdapters, TType> { private readonly settings; /** * @example * ```ts * import { CacheFactory } from "@daiso-tech/core/cache"; * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/memory-cache-adapter"; * import { RedisCacheAdapter } from "@daiso-tech/core/cache/redis-cache-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import type { ISerde } from "@daiso-tech/core/serde/contracts"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import Redis from "ioredis" * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const cacheFactory = new CacheFactory({ * adapters: { * memory: new MemoryCacheAdapter(), * redis: new RedisCacheAdapter({ * database: new Redis("YOUR_REDIS_CONNECTION"), * serde, * }), * }, * defaultAdapter: "memory", * }); */ constructor(settings: CacheFactorySettings<TAdapters, TType>); setNamespace(namespace: Namespace): CacheFactory<TAdapters, TType>; setDefaultTtl(ttl: ITimeSpan): CacheFactory<TAdapters, TType>; setEventBus(eventBus: IEventBus): CacheFactory<TAdapters, TType>; setSchema<TSchemaOutputType>(schema: StandardSchemaV1<TSchemaOutputType>): CacheFactory<TAdapters, TSchemaOutputType>; setType<TOutputType>(): CacheFactory<TAdapters, TOutputType>; setJitter(jitter: number): CacheFactory<TAdapters, TType>; /** * @example * ```ts * import { CacheFactory } from "@daiso-tech/core/cache"; * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/memory-cache-adapter"; * import { RedisCacheAdapter } from "@daiso-tech/core/cache/redis-cache-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import type { ISerde } from "@daiso-tech/core/serde/contracts"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span"; * import Redis from "ioredis" * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const cacheFactory = new CacheFactory({ * adapters: { * memory: new MemoryCacheAdapter(), * redis: new RedisCacheAdapter({ * database: new Redis("YOUR_REDIS_CONNECTION"), * serde, * }), * }, * defaultAdapter: "memory", * }); * * // Will add key to cache using the default adapter which is MemoryCacheAdapter * await cacheFactory * .use() * .add("a", 1); * * // Will add key to cache using the redis adapter * await cacheFactory * .use("redis") * .add("a", 1); * * // You can change the default settings of the returned Cache instance. * await cacheFactory * .setDefaultTtl(TimeSpan.fromMinutes(2)) * .use("sqlite") * .add("a", 1); * ``` */ use(adapterName?: TAdapters | undefined): ICache<TType>; }