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.

105 lines (104 loc) 4.25 kB
/** * @module Cache */ import { type AsyncLazy, type Factory } from "../../../../utilities/_module-exports.js"; import type { IEventBus } from "../../../../event-bus/contracts/_module-exports.js"; import type { ICache, ICacheFactory } from "../../../../cache/contracts/_module-exports.js"; import { type CacheSettingsBase, type CacheAdapter } from "../../../../cache/implementations/derivables/cache/_module.js"; import { Namespace, type TimeSpan } from "../../../../utilities/_module-exports.js"; import type { LazyPromise } from "../../../../async/_module-exports.js"; import type { StandardSchemaV1 } from "@standard-schema/spec"; /** * * IMPORT_PATH: `"@daiso-tech/core/cache"` * @group Derivables */ export type CacheAdapters<TAdapters extends string = string> = Partial<Record<TAdapters, CacheAdapter<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, RedisCacheAdapter } from "@daiso-tech/core/cache/adapters"; * import { Serde } from "@daiso-tech/core/serde"; * import type { ISerde } from "@daiso-tech/core/serde/contracts"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"; * 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: TimeSpan): CacheFactory<TAdapters, TType>; setEventBus(eventBus: IEventBus): CacheFactory<TAdapters, TType>; setLazyPromiseFactory(factory: Factory<AsyncLazy<any>, LazyPromise<any>>): CacheFactory<TAdapters, TType>; setSchema<TSchemaOutputType>(schema: StandardSchemaV1<TSchemaOutputType>): CacheFactory<TAdapters, TSchemaOutputType>; setType<TOutputType>(): CacheFactory<TAdapters, TOutputType>; /** * @example * ```ts * import { CacheFactory } from "@daiso-tech/core/cache"; * import { MemoryCacheAdapter, RedisCacheAdapter } from "@daiso-tech/core/cache/adapters"; * import { Serde } from "@daiso-tech/core/serde"; * import type { ISerde } from "@daiso-tech/core/serde/contracts"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"; * import { TimeSpan } from "@daiso-tech/core/utilities"; * 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 which is RedisCacheAdapter * 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>; }