UNPKG

@iocium/cachekit

Version:

A pluggable, backend-agnostic caching adapter for Node.js and serverless platforms

112 lines (104 loc) 3.18 kB
/** * Represents a cache entry, with optional expiration timestamp. */ interface CacheRecord { value: any; expiresAt?: number; } /** * A backend interface that defines required cache operations. */ interface CacheBackend { get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * A user-facing cache API for setting and retrieving values. */ interface CacheKit { get(key: string): Promise<any>; set(key: string, value: any, ttlMs?: number): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * Configuration options for creating a cache instance. */ interface CacheKitOptions { backend?: CacheBackend; } /** * Creates a new instance of an CacheKit with the specified backend. * Defaults to an in-memory backend if none is provided. * * @param options Optional configuration object specifying the backend. * @returns An CacheKit interface instance. */ declare function createCacheKit(options?: CacheKitOptions): CacheKit; /** * A simple in-memory cache backend using Map. */ declare class MemoryBackend implements CacheBackend { private store; get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * A Cloudflare KV-based backend implementation. */ declare class KVBackend implements CacheBackend { private kv; constructor(kv: KVNamespace); get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * A Cloudflare D1 backend using SQLite-like SQL syntax. */ declare class D1Backend implements CacheBackend { private db; constructor(db: D1Database); get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * A Redis-backed cache implementation. */ declare class RedisBackend implements CacheBackend { private redis; constructor(redis: { get: Function; set: Function; del: Function; flushall: Function; }); get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } /** * A Memcached-based cache implementation. */ declare class MemcachedBackend implements CacheBackend { private memcached; constructor(memcached: { get: Function; set: Function; del: Function; flush: Function; }); get(key: string): Promise<CacheRecord | undefined>; set(key: string, record: CacheRecord): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; } export { type CacheBackend, type CacheKit, type CacheKitOptions, type CacheRecord, D1Backend, KVBackend, MemcachedBackend, MemoryBackend, RedisBackend, createCacheKit };