From c28c8d18887347630ebdcacbf54f0d7f40defbdc Mon Sep 17 00:00:00 2001 From: yasser Date: Tue, 31 Mar 2026 10:08:38 +0100 Subject: [PATCH 1/8] feat(COMPT-55): add ICacheStore port and Redis/InMemory adapters --- jest.config.ts | 2 + package-lock.json | 92 ++++++++++- package.json | 5 +- src/adapters/in-memory-cache-store.adapter.ts | 120 ++++++++++++++ src/adapters/redis-cache-store.adapter.ts | 156 ++++++++++++++++++ src/index.ts | 20 +++ src/ports/cache-store.port.ts | 58 +++++++ tsconfig.json | 4 +- 8 files changed, 451 insertions(+), 6 deletions(-) create mode 100644 src/adapters/in-memory-cache-store.adapter.ts create mode 100644 src/adapters/redis-cache-store.adapter.ts create mode 100644 src/ports/cache-store.port.ts diff --git a/jest.config.ts b/jest.config.ts index 1d7bc2e..958fd0b 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -46,6 +46,8 @@ const config: Config = { "^@filters/(.*)$": "/src/filters/$1", "^@middleware/(.*)$": "/src/middleware/$1", "^@utils/(.*)$": "/src/utils/$1", + "^@ports/(.*)$": "/src/ports/$1", + "^@adapters/(.*)$": "/src/adapters/$1", }, }; diff --git a/package-lock.json b/package-lock.json index ff750d7..cc2bc3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "MIT", "dependencies": { "class-transformer": "^0.5.1", - "class-validator": "^0.14.1" + "class-validator": "^0.14.1", + "ioredis": "^5.10.1" }, "devDependencies": { "@changesets/cli": "^2.27.7", @@ -1362,6 +1363,12 @@ } } }, + "node_modules/@ioredis/commands": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.5.1.tgz", + "integrity": "sha512-JH8ZL/ywcJyR9MmJ5BNqZllXNZQqQbnVZOqpPQqE1vHiFgAw4NHbvE0FOduNU8IX9babitBT46571OnPTT0Zcw==", + "license": "MIT" + }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -3909,6 +3916,15 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, + "node_modules/cluster-key-slot": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz", + "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -4140,7 +4156,6 @@ "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", - "dev": true, "license": "MIT", "dependencies": { "ms": "^2.1.3" @@ -4222,6 +4237,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -5904,6 +5928,30 @@ "node": ">= 0.4" } }, + "node_modules/ioredis": { + "version": "5.10.1", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.10.1.tgz", + "integrity": "sha512-HuEDBTI70aYdx1v6U97SbNx9F1+svQKBDo30o0b9fw055LMepzpOOd0Ccg9Q6tbqmBSJaMuY0fB7yw9/vjBYCA==", + "license": "MIT", + "dependencies": { + "@ioredis/commands": "1.5.1", + "cluster-key-slot": "^1.1.0", + "debug": "^4.3.4", + "denque": "^2.1.0", + "lodash.defaults": "^4.2.0", + "lodash.isarguments": "^3.1.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0", + "standard-as-callback": "^2.1.0" + }, + "engines": { + "node": ">=12.22.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/ioredis" + } + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -7348,6 +7396,18 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash.defaults": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", + "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==", + "license": "MIT" + }, + "node_modules/lodash.isarguments": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", + "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==", + "license": "MIT" + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -7670,7 +7730,6 @@ "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", - "dev": true, "license": "MIT" }, "node_modules/multer": { @@ -8592,6 +8651,27 @@ "node": ">= 6" } }, + "node_modules/redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==", + "license": "MIT", + "dependencies": { + "redis-errors": "^1.0.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/reflect-metadata": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz", @@ -9238,6 +9318,12 @@ "node": ">=8" } }, + "node_modules/standard-as-callback": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.1.0.tgz", + "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==", + "license": "MIT" + }, "node_modules/statuses": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", diff --git a/package.json b/package.json index 13f6181..fd7b393 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,8 @@ }, "dependencies": { "class-transformer": "^0.5.1", - "class-validator": "^0.14.1" + "class-validator": "^0.14.1", + "ioredis": "^5.10.1" }, "devDependencies": { "@changesets/cli": "^2.27.7", @@ -79,4 +80,4 @@ "typescript": "^5.7.3", "typescript-eslint": "^8.50.1" } -} \ No newline at end of file +} diff --git a/src/adapters/in-memory-cache-store.adapter.ts b/src/adapters/in-memory-cache-store.adapter.ts new file mode 100644 index 0000000..e78d36c --- /dev/null +++ b/src/adapters/in-memory-cache-store.adapter.ts @@ -0,0 +1,120 @@ +/** + * @file in-memory-cache-store.adapter.ts + * + * In-memory implementation of ICacheStore backed by a plain JavaScript Map. + * + * Behaviour: + * - Values are JSON-serialized on write and JSON-parsed on read, matching + * the Redis adapter exactly so both can be swapped transparently. + * - TTL is enforced lazily: an expired entry is evicted the first time it + * is read, rather than via a background sweep timer. + * - A parse failure (malformed JSON) returns null instead of throwing. + * - No external dependencies — suitable for unit tests, local development, + * or lightweight production usage that does not require persistence. + * + * Exports: + * - CacheEntry → internal shape of stored entries (exported for tests) + * - InMemoryCacheStore → the concrete in-memory adapter class + */ + +import type { ICacheStore } from "@ports/cache-store.port"; + +// --------------------------------------------------------------------------- +// Internal data shape +// --------------------------------------------------------------------------- + +/** + * Shape of each entry held inside the backing Map. + * Exported so that unit tests can inspect the internal store if needed. + */ +export interface CacheEntry { + /** JSON-serialized representation of the cached value */ + value: string; + + /** + * Absolute Unix timestamp (ms) at which this entry expires. + * null means the entry never expires. + */ + expiresAt: number | null; +} + +// --------------------------------------------------------------------------- +// Adapter +// --------------------------------------------------------------------------- + +/** + * In-memory adapter for the ICacheStore port. + * + * Usage: + * ```typescript + * const store = new InMemoryCacheStore(); + * await store.set("session:abc", { userId: 1 }, 60); // expires in 60 s + * const session = await store.get("session:abc"); + * ``` + */ +export class InMemoryCacheStore implements ICacheStore { + /** + * The backing store. + * Maps every cache key to its serialized value and optional expiry timestamp. + */ + private readonly store = new Map(); + + // --------------------------------------------------------------------------- + // ICacheStore implementation + // --------------------------------------------------------------------------- + + /** {@inheritDoc ICacheStore.get} */ + async get(key: string): Promise { + // Look up the entry — undefined means the key was never set or was deleted + const entry = this.store.get(key); + + // Key does not exist in the store + if (entry === undefined) return null; + + // Lazy TTL expiry: check whether the entry has passed its deadline. + // Date.now() returns the current time in milliseconds. + if (entry.expiresAt !== null && Date.now() > entry.expiresAt) { + // Remove the stale entry and treat the lookup as a cache miss + this.store.delete(key); + return null; + } + + // Deserialize the stored JSON string back to the caller's expected type. + // Return null on malformed JSON instead of propagating a SyntaxError. + try { + return JSON.parse(entry.value) as T; + } catch { + // Parse failure — treat as a cache miss + return null; + } + } + + /** {@inheritDoc ICacheStore.set} */ + async set(key: string, value: T, ttlSeconds?: number): Promise { + // Compute the absolute expiry timestamp from the relative TTL. + // Multiply seconds by 1 000 to convert to milliseconds for Date.now() comparison. + // null signals "no expiry" so the entry lives until deleted or clear() is called. + const expiresAt = + ttlSeconds !== undefined && ttlSeconds > 0 + ? Date.now() + ttlSeconds * 1_000 + : null; + + // Serialize the value to a JSON string before storing to match Redis adapter behaviour + this.store.set(key, { + value: JSON.stringify(value), + expiresAt, + }); + } + + /** {@inheritDoc ICacheStore.delete} */ + async delete(key: string): Promise { + // Map.delete is a no-op when the key does not exist — no guard required + this.store.delete(key); + } + + /** {@inheritDoc ICacheStore.clear} */ + async clear(): Promise { + // Remove every entry from the backing Map in O(1) + this.store.clear(); + } +} diff --git a/src/adapters/redis-cache-store.adapter.ts b/src/adapters/redis-cache-store.adapter.ts new file mode 100644 index 0000000..f884015 --- /dev/null +++ b/src/adapters/redis-cache-store.adapter.ts @@ -0,0 +1,156 @@ +/** + * @file redis-cache-store.adapter.ts + * + * Redis-backed implementation of ICacheStore, built on top of the ioredis client. + * + * Behaviour: + * - Values are JSON-serialized on write and JSON-parsed on read. + * - A parse failure (malformed JSON) returns null instead of throwing. + * - An optional key prefix namespaces every key so multiple adapters can + * share the same Redis database without colliding. + * - clear() only removes keys that belong to this adapter's prefix; + * without a prefix it flushes the entire Redis database (FLUSHDB). + * + * Exports: + * - RedisCacheStoreOptions → configuration shape for the constructor + * - RedisCacheStore → the concrete Redis adapter class + */ + +import Redis from "ioredis"; + +import type { ICacheStore } from "@ports/cache-store.port"; + +// --------------------------------------------------------------------------- +// Configuration +// --------------------------------------------------------------------------- + +/** + * Constructor options for RedisCacheStore. + */ +export interface RedisCacheStoreOptions { + /** + * An already-constructed ioredis client, OR a Redis connection URL string. + * Passing an existing client lets the caller manage the connection lifecycle. + * Passing a URL string creates a new internal client automatically. + * + * @example "redis://localhost:6379" + */ + client: Redis | string; + + /** + * Optional prefix prepended to every key as ":". + * Useful for isolating cache namespaces on a shared Redis instance. + * + * @example "myapp:cache" + */ + keyPrefix?: string; +} + +// --------------------------------------------------------------------------- +// Adapter +// --------------------------------------------------------------------------- + +/** + * Redis adapter for the ICacheStore port. + * + * Usage: + * ```typescript + * const store = new RedisCacheStore({ client: "redis://localhost:6379", keyPrefix: "app" }); + * await store.set("user:1", { name: "Alice" }, 300); // TTL 5 min + * const user = await store.get("user:1"); + * ``` + */ +export class RedisCacheStore implements ICacheStore { + /** Underlying ioredis client used for all Redis commands */ + private readonly redis: Redis; + + /** Key prefix applied to every cache key (may be an empty string) */ + private readonly keyPrefix: string; + + constructor(options: RedisCacheStoreOptions) { + // Accept either an existing ioredis client or a plain connection URL string. + // When a URL is provided we create a new dedicated client instance. + this.redis = + typeof options.client === "string" ? new Redis(options.client) : options.client; + + // Fall back to an empty string so buildKey() can skip the prefix logic. + this.keyPrefix = options.keyPrefix ?? ""; + } + + // --------------------------------------------------------------------------- + // Private helpers + // --------------------------------------------------------------------------- + + /** + * Prepend the adapter's namespace prefix to a key. + * Returns the key unchanged when no prefix was configured. + * + * @param key - Raw cache key + * @returns Full Redis key with optional prefix + */ + private buildKey(key: string): string { + // Only add the colon separator when a prefix is set + return this.keyPrefix ? `${this.keyPrefix}:${key}` : key; + } + + // --------------------------------------------------------------------------- + // ICacheStore implementation + // --------------------------------------------------------------------------- + + /** {@inheritDoc ICacheStore.get} */ + async get(key: string): Promise { + // Fetch the raw serialized string from Redis (returns null if key is missing) + const raw = await this.redis.get(this.buildKey(key)); + + // Key does not exist in Redis — return null immediately + if (raw === null) return null; + + // Deserialize the JSON string back to the caller's expected type. + // If the stored value is somehow malformed, return null instead of crashing. + try { + return JSON.parse(raw) as T; + } catch { + // Parse failure — treat as a cache miss + return null; + } + } + + /** {@inheritDoc ICacheStore.set} */ + async set(key: string, value: T, ttlSeconds?: number): Promise { + // Serialize the value to a JSON string before handing it to Redis + const serialized = JSON.stringify(value); + const fullKey = this.buildKey(key); + + if (ttlSeconds !== undefined && ttlSeconds > 0) { + // EX flag sets the expiry in seconds alongside the value in a single command + await this.redis.set(fullKey, serialized, "EX", ttlSeconds); + } else { + // No TTL requested — key persists until explicitly deleted or clear() is called + await this.redis.set(fullKey, serialized); + } + } + + /** {@inheritDoc ICacheStore.delete} */ + async delete(key: string): Promise { + // DEL is a no-op in Redis when the key does not exist, so no guard is needed + await this.redis.del(this.buildKey(key)); + } + + /** {@inheritDoc ICacheStore.clear} */ + async clear(): Promise { + if (this.keyPrefix) { + // Prefix mode: collect only the keys that belong to this adapter's namespace. + // NOTE: KEYS is O(N) and blocks Redis — acceptable for dev / low-traffic scenarios. + // Consider SCAN-based iteration for high-traffic production deployments. + const keys = await this.redis.keys(`${this.keyPrefix}:*`); + + // Only call DEL when there is at least one matching key + if (keys.length > 0) { + await this.redis.del(...keys); + } + } else { + // No prefix — flush every key in the currently selected Redis database + await this.redis.flushdb(); + } + } +} diff --git a/src/index.ts b/src/index.ts index 3026198..ad890ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,26 @@ export { ExampleData, ExampleParam } from "./decorators/example.decorator"; // Export types and interfaces for TypeScript consumers // export type { YourCustomType } from './types'; +// ============================================================================ +// PORTS (Abstractions / Interfaces) +// ============================================================================ +// Export the ICacheStore interface so consumers can type their own adapters +// or declare injection tokens without depending on a concrete implementation. +export type { ICacheStore } from "./ports/cache-store.port"; + +// ============================================================================ +// ADAPTERS (Concrete Cache Store Implementations) +// ============================================================================ +// Both adapters implement ICacheStore — consumers choose the one that fits their stack. + +// Redis-backed adapter — requires the "ioredis" peer dependency. +export { RedisCacheStore } from "./adapters/redis-cache-store.adapter"; +export type { RedisCacheStoreOptions } from "./adapters/redis-cache-store.adapter"; + +// In-memory adapter — zero external dependencies; ideal for tests and local dev. +export { InMemoryCacheStore } from "./adapters/in-memory-cache-store.adapter"; +export type { CacheEntry } from "./adapters/in-memory-cache-store.adapter"; + // ============================================================================ // ❌ NEVER EXPORT (Internal Implementation) // ============================================================================ diff --git a/src/ports/cache-store.port.ts b/src/ports/cache-store.port.ts new file mode 100644 index 0000000..34e2e33 --- /dev/null +++ b/src/ports/cache-store.port.ts @@ -0,0 +1,58 @@ +/** + * @file cache-store.port.ts + * + * Defines the ICacheStore port — the single contract every cache adapter must implement. + * By depending only on this interface (not on Redis, Map, or any concrete client), + * the rest of the codebase stays decoupled from storage details. + * + * Exports: + * - ICacheStore → generic cache interface (get / set / delete / clear) + */ + +/** + * Generic, Promise-based cache store interface. + * + * All four operations are async so that both in-memory and network-backed + * (e.g. Redis) adapters can satisfy the same contract without blocking. + * + * Concrete implementations live in src/adapters/: + * - RedisCacheStore — backed by ioredis + * - InMemoryCacheStore — backed by a plain Map + Date.now() TTL + */ +export interface ICacheStore { + /** + * Retrieve and deserialize a cached value. + * + * Returns null when: + * - the key does not exist + * - the entry has expired (TTL elapsed) + * - the stored value cannot be parsed (malformed JSON) + * + * @param key - Unique cache key + * @returns The deserialized value, or null + */ + get(key: string): Promise; + + /** + * Serialize and store a value under the given key. + * + * @param key - Unique cache key + * @param value - Any JSON-serializable value + * @param ttlSeconds - Optional time-to-live in seconds; omit or pass 0 for no expiry + */ + set(key: string, value: T, ttlSeconds?: number): Promise; + + /** + * Remove a single entry from the cache. + * Silently succeeds if the key does not exist. + * + * @param key - Cache key to remove + */ + delete(key: string): Promise; + + /** + * Evict every entry from the cache. + * After this call the store is empty (equivalent to a full flush). + */ + clear(): Promise; +} diff --git a/tsconfig.json b/tsconfig.json index e92d316..2010ba7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,7 +30,9 @@ "@config/*": ["src/config/*"], "@filters/*": ["src/filters/*"], "@middleware/*": ["src/middleware/*"], - "@utils/*": ["src/utils/*"] + "@utils/*": ["src/utils/*"], + "@ports/*": ["src/ports/*"], + "@adapters/*": ["src/adapters/*"] } }, "include": ["src/**/*.ts", "test/**/*.ts"], From 529604deb6cd234b33c608952a42ffeb66052798 Mon Sep 17 00:00:00 2001 From: yasser Date: Wed, 1 Apr 2026 09:13:13 +0100 Subject: [PATCH 2/8] feat(COMPT-56): add CacheModule, CacheService, and DI tokens --- src/cache-kit.module.ts | 262 ++++++++++++++++++++++++++++++++++ src/constants.ts | 38 +++++ src/index.ts | 18 ++- src/services/cache.service.ts | 189 ++++++++++++++++++++++++ 4 files changed, 503 insertions(+), 4 deletions(-) create mode 100644 src/cache-kit.module.ts create mode 100644 src/constants.ts create mode 100644 src/services/cache.service.ts diff --git a/src/cache-kit.module.ts b/src/cache-kit.module.ts new file mode 100644 index 0000000..f49472c --- /dev/null +++ b/src/cache-kit.module.ts @@ -0,0 +1,262 @@ +/** + * @file cache-kit.module.ts + * + * CacheModule — the top-level NestJS dynamic module for CacheKit. + * + * Responsibilities: + * - Accept configuration (store type, default TTL, provider-specific options) + * via either a synchronous `register()` or an asynchronous `registerAsync()` call. + * - Instantiate the correct ICacheStore adapter (RedisCacheStore or InMemoryCacheStore) + * based on the `store` option and register it under the CACHE_STORE DI token. + * - Register CacheService and export it so consuming modules can inject it. + * + * Exports: + * - CacheModuleOptions → synchronous configuration shape + * - CacheModuleAsyncOptions → asynchronous configuration shape (useFactory / useClass / useExisting) + * - CacheModule → the NestJS dynamic module class + */ + +import { DynamicModule, Module, Provider, Type } from "@nestjs/common"; + +import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; +import { RedisCacheStore } from "@adapters/redis-cache-store.adapter"; +import type { RedisCacheStoreOptions } from "@adapters/redis-cache-store.adapter"; +import type { ICacheStore } from "@ports/cache-store.port"; + +import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "./constants"; +import { CacheService } from "./services/cache.service"; + +// --------------------------------------------------------------------------- +// Configuration interfaces +// --------------------------------------------------------------------------- + +/** + * Synchronous configuration options for CacheModule.register(). + */ +export interface CacheModuleOptions { + /** + * Which backing store to use. + * - "redis" → RedisCacheStore (requires the `redis` field) + * - "memory" → InMemoryCacheStore (no extra config needed) + */ + store: "redis" | "memory"; + + /** + * Default time-to-live in seconds applied to every CacheService.set() call + * that does not supply its own TTL. + * Omit or set to 0 for no default expiry. + */ + ttl?: number; + + /** + * Redis adapter configuration — required when store is "redis". + * Ignored when store is "memory". + */ + redis?: RedisCacheStoreOptions; +} + +/** + * Factory function type used by registerAsync's useFactory. + * May return the options synchronously or as a Promise. + */ +export type CacheModuleOptionsFactory = () => + | Promise + | CacheModuleOptions; + +/** + * Asynchronous configuration options for CacheModule.registerAsync(). + * Supports three patterns: + * - useFactory — inline factory function (most common) + * - useClass — instantiate a config class per module + * - useExisting — reuse an already-provided config class + */ +export interface CacheModuleAsyncOptions { + /** Providers whose tokens are passed as arguments to useFactory. */ + inject?: any[]; + + /** Inline factory that resolves to CacheModuleOptions. */ + useFactory?: (...args: any[]) => Promise | CacheModuleOptions; + + /** + * Class that the module will instantiate to obtain the options. + * The class must implement CacheModuleOptionsFactory. + */ + useClass?: Type<{ createCacheOptions(): Promise | CacheModuleOptions }>; + + /** + * Re-use an already-provided token (class or value) as the options factory. + * The resolved instance must implement CacheModuleOptionsFactory. + */ + useExisting?: Type<{ createCacheOptions(): Promise | CacheModuleOptions }>; + + /** Additional NestJS modules to import into the async provider scope. */ + imports?: any[]; +} + +// --------------------------------------------------------------------------- +// Internal factory helpers +// --------------------------------------------------------------------------- + +/** + * Build the ICacheStore provider from a resolved CacheModuleOptions object. + * This is the single place where we decide which adapter to create. + * + * @param options - Fully resolved module options + * @returns The adapter instance typed as ICacheStore + */ +function createStoreFromOptions(options: CacheModuleOptions): ICacheStore { + if (options.store === "redis") { + // Redis store requires connection details — throw early with a clear message + // rather than letting ioredis surface a confusing low-level error. + if (!options.redis) { + throw new Error( + '[CacheModule] store is "redis" but no redis options were provided. ' + + 'Pass a `redis` field to CacheModule.register() or CacheModule.registerAsync().', + ); + } + // Delegate all Redis connection and key-prefix logic to the adapter + return new RedisCacheStore(options.redis); + } + + // Default: in-memory store — zero dependencies, no extra options needed + return new InMemoryCacheStore(); +} + +/** + * Build the CACHE_MODULE_OPTIONS and CACHE_STORE providers for the + * registerAsync path, handling all three async patterns. + * + * @param options - Async configuration options + * @returns Array of NestJS providers ready to be registered + */ +function createAsyncProviders(options: CacheModuleAsyncOptions): Provider[] { + // ── useFactory ───────────────────────────────────────────────────────── + if (options.useFactory) { + return [ + { + // Resolve the options object asynchronously via the factory + provide: CACHE_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject ?? [], + }, + { + // Once options are resolved, build the correct store adapter + provide: CACHE_STORE, + useFactory: (resolvedOptions: CacheModuleOptions): ICacheStore => + createStoreFromOptions(resolvedOptions), + inject: [CACHE_MODULE_OPTIONS], + }, + ]; + } + + // ── useClass / useExisting ────────────────────────────────────────────── + const factoryClass = (options.useClass ?? options.useExisting)!; + + const factoryProvider: Provider = options.useClass + ? // useClass: let NestJS instantiate a new instance of this class + { provide: factoryClass, useClass: factoryClass } + : // useExisting: reuse a token already registered elsewhere in the module tree + { provide: factoryClass, useExisting: options.useExisting }; + + return [ + factoryProvider, + { + // Call createCacheOptions() on the factory instance to get the options + provide: CACHE_MODULE_OPTIONS, + useFactory: (factory: { createCacheOptions(): Promise | CacheModuleOptions }) => + factory.createCacheOptions(), + inject: [factoryClass], + }, + { + // Build the store adapter from the resolved options + provide: CACHE_STORE, + useFactory: (resolvedOptions: CacheModuleOptions): ICacheStore => + createStoreFromOptions(resolvedOptions), + inject: [CACHE_MODULE_OPTIONS], + }, + ]; +} + +// --------------------------------------------------------------------------- +// Module +// --------------------------------------------------------------------------- + +/** + * CacheModule — dynamic NestJS module providing CacheService to the host app. + * + * @example Synchronous registration + * ```typescript + * CacheModule.register({ store: 'memory', ttl: 60 }) + * CacheModule.register({ store: 'redis', ttl: 300, redis: { client: 'redis://localhost:6379' } }) + * ``` + * + * @example Async registration with ConfigService + * ```typescript + * CacheModule.registerAsync({ + * imports: [ConfigModule], + * inject: [ConfigService], + * useFactory: (cfg: ConfigService) => ({ + * store: cfg.get('CACHE_STORE'), + * ttl: cfg.get('CACHE_TTL'), + * redis: { client: cfg.get('REDIS_URL') }, + * }), + * }) + * ``` + */ +@Module({}) +export class CacheModule { + /** + * Register the module with synchronous, inline configuration. + * + * @param options - Cache configuration (store type, default TTL, redis options) + * @returns Configured DynamicModule + */ + static register(options: CacheModuleOptions): DynamicModule { + const providers: Provider[] = [ + // Expose the raw options object for injection (e.g. CacheService reads ttl from here) + { + provide: CACHE_MODULE_OPTIONS, + useValue: options, + }, + // Build and register the correct adapter under the CACHE_STORE token + { + provide: CACHE_STORE, + useValue: createStoreFromOptions(options), + }, + // The main service consumers will inject + CacheService, + ]; + + return { + module: CacheModule, + providers, + // Export CacheService so the importing module's children can use it + exports: [CacheService, CACHE_STORE], + }; + } + + /** + * Register the module with asynchronous configuration — useful when options + * must come from ConfigService, environment variables resolved at runtime, etc. + * + * Supports useFactory, useClass, and useExisting patterns. + * + * @param options - Async configuration options + * @returns Configured DynamicModule + */ + static registerAsync(options: CacheModuleAsyncOptions): DynamicModule { + // Build CACHE_MODULE_OPTIONS + CACHE_STORE providers depending on async pattern used + const asyncProviders = createAsyncProviders(options); + + return { + module: CacheModule, + // Import any modules required by the factory (e.g. ConfigModule) + imports: options.imports ?? [], + providers: [ + ...asyncProviders, + CacheService, + ], + exports: [CacheService, CACHE_STORE], + }; + } +} diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..4372bac --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,38 @@ +/** + * @file constants.ts + * + * NestJS dependency-injection tokens used throughout the CacheKit module. + * + * Exporting tokens from this file lets both the module wiring and any + * consumer code reference the same string without risk of typos. + * + * Exports: + * - CACHE_STORE → token for the ICacheStore adapter provider + * - CACHE_MODULE_OPTIONS → token for the CacheModuleOptions configuration provider + */ + +/** + * DI token for the active ICacheStore adapter. + * + * The module registers whichever adapter was selected (Redis or InMemory) + * under this token so CacheService can inject it without knowing the concrete type. + * + * @example + * ```typescript + * @Inject(CACHE_STORE) private readonly store: ICacheStore + * ``` + */ +export const CACHE_STORE = "CACHE_STORE" as const; + +/** + * DI token for the CacheModuleOptions configuration object. + * + * CacheService uses this to read the default TTL when the caller does not + * supply a per-call TTL. + * + * @example + * ```typescript + * @Inject(CACHE_MODULE_OPTIONS) private readonly options: CacheModuleOptions + * ``` + */ +export const CACHE_MODULE_OPTIONS = "CACHE_MODULE_OPTIONS" as const; diff --git a/src/index.ts b/src/index.ts index ad890ee..3ff9c23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,14 +10,24 @@ import "reflect-metadata"; // ============================================================================ // MODULE // ============================================================================ -export { ExampleKitModule } from "./example-kit.module"; -export type { ExampleKitOptions, ExampleKitAsyncOptions } from "./example-kit.module"; +// CacheModule — the main dynamic module consumers import into their AppModule. +// Supports both synchronous (register) and asynchronous (registerAsync) setup. +export { CacheModule } from "./cache-kit.module"; +export type { CacheModuleOptions, CacheModuleAsyncOptions } from "./cache-kit.module"; + +// ============================================================================ +// DI TOKENS +// ============================================================================ +// Exported so consumers can inject the raw ICacheStore directly if needed, +// or reference CACHE_STORE in their own provider definitions. +export { CACHE_STORE, CACHE_MODULE_OPTIONS } from "./constants"; // ============================================================================ // SERVICES (Main API) // ============================================================================ -// Export services that consumers will interact with -export { ExampleService } from "./services/example.service"; +// CacheService is the primary interface consumers interact with. +// Inject it anywhere via constructor injection. +export { CacheService } from "./services/cache.service"; // ============================================================================ // DTOs (Public Contracts) diff --git a/src/services/cache.service.ts b/src/services/cache.service.ts new file mode 100644 index 0000000..3157e27 --- /dev/null +++ b/src/services/cache.service.ts @@ -0,0 +1,189 @@ +/** + * @file cache.service.ts + * + * CacheService — the primary API that consumers inject into their NestJS services. + * + * Wraps the active ICacheStore adapter and adds: + * - Default TTL fall-through from module options + * - `has()` — existence check without deserialization overhead + * - `wrap()` — cache-aside pattern: return cached value or compute, store, and return it + * + * Exports: + * - CacheService → injectable NestJS service + */ + +import { Inject, Injectable } from "@nestjs/common"; + +import type { ICacheStore } from "@ports/cache-store.port"; + +import type { CacheModuleOptions } from "../cache-kit.module"; +import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "../constants"; + +/** + * Injectable caching service. + * + * Inject this in your own services: + * ```typescript + * constructor(private readonly cache: CacheService) {} + * ``` + */ +@Injectable() +export class CacheService { + constructor( + /** The active store adapter (Redis or InMemory) registered under CACHE_STORE */ + @Inject(CACHE_STORE) + private readonly store: ICacheStore, + + /** Module-level options — used to read the default TTL */ + @Inject(CACHE_MODULE_OPTIONS) + private readonly options: CacheModuleOptions, + ) {} + + // --------------------------------------------------------------------------- + // Core operations + // --------------------------------------------------------------------------- + + /** + * Retrieve a value from the cache. + * + * Returns null when the key is missing, the entry is expired, + * or the stored value cannot be parsed. + * + * @param key - Cache key + * @returns The cached value, or null + * + * @example + * ```typescript + * const user = await this.cache.get('user:1'); + * ``` + */ + async get(key: string): Promise { + // Delegate entirely to the adapter — no extra logic here + return this.store.get(key); + } + + /** + * Store a value in the cache. + * + * The TTL resolution order is: + * 1. `ttlSeconds` argument (explicit per-call TTL) + * 2. `options.ttl` supplied to CacheModule.register() (module default) + * 3. No expiry (value lives until explicitly deleted or clear() is called) + * + * @param key - Cache key + * @param value - Any JSON-serializable value + * @param ttlSeconds - Optional per-call TTL; overrides the module default + * + * @example + * ```typescript + * await this.cache.set('user:1', user, 300); // 5-minute TTL + * ``` + */ + async set(key: string, value: T, ttlSeconds?: number): Promise { + // Use the per-call TTL when provided; fall back to the module-level default + const effectiveTtl = ttlSeconds ?? this.options.ttl; + return this.store.set(key, value, effectiveTtl); + } + + /** + * Remove a single entry from the cache. + * Silently succeeds if the key does not exist. + * + * @param key - Cache key to remove + * + * @example + * ```typescript + * await this.cache.delete('user:1'); + * ``` + */ + async delete(key: string): Promise { + return this.store.delete(key); + } + + /** + * Evict every entry from the cache. + * + * @example + * ```typescript + * await this.cache.clear(); + * ``` + */ + async clear(): Promise { + return this.store.clear(); + } + + // --------------------------------------------------------------------------- + // Convenience helpers + // --------------------------------------------------------------------------- + + /** + * Check whether a non-expired entry exists for the given key. + * + * Internally performs a full get() — the value is fetched and parsed but + * then discarded. For frequent hot-path checks consider caching the boolean + * result if the underlying store does not have a native EXISTS command. + * + * @param key - Cache key to check + * @returns true if the key exists and has not expired, false otherwise + * + * @example + * ```typescript + * if (await this.cache.has('rate-limit:user:1')) { ... } + * ``` + */ + async has(key: string): Promise { + // A null result from get() means "does not exist or is expired" + const value = await this.store.get(key); + return value !== null; + } + + /** + * Cache-aside helper: return the cached value if it exists, + * otherwise call `fn`, persist its result, and return it. + * + * This is the recommended way to lazily populate the cache: + * ``` + * cached? ──yes──▶ return cached value + * │ + * no + * │ + * call fn() ──▶ store result ──▶ return result + * ``` + * + * TTL resolution is the same as set(): + * 1. `ttlSeconds` argument + * 2. Module-level default (`options.ttl`) + * 3. No expiry + * + * @param key - Cache key + * @param fn - Async factory that produces the value on a cache miss + * @param ttlSeconds - Optional per-call TTL; overrides the module default + * @returns The cached or freshly computed value + * + * @example + * ```typescript + * const user = await this.cache.wrap( + * `user:${id}`, + * () => this.userRepository.findById(id), + * 60, + * ); + * ``` + */ + async wrap(key: string, fn: () => Promise, ttlSeconds?: number): Promise { + // Step 1: Try the cache first + const cached = await this.store.get(key); + + // Cache hit — return the stored value without calling fn() + if (cached !== null) return cached; + + // Cache miss — compute the fresh value by executing the factory function + const fresh = await fn(); + + // Persist the result so the next caller hits the cache + // Use the per-call TTL when provided; fall back to the module-level default + const effectiveTtl = ttlSeconds ?? this.options.ttl; + await this.store.set(key, fresh, effectiveTtl); + + return fresh; + } +} From 56a19a1fb47ccdb71d1385bc72536f36fe65ead9 Mon Sep 17 00:00:00 2001 From: yasser Date: Wed, 1 Apr 2026 09:48:54 +0100 Subject: [PATCH 3/8] style: fix Prettier formatting across all files --- src/adapters/in-memory-cache-store.adapter.ts | 4 +--- src/adapters/redis-cache-store.adapter.ts | 3 +-- src/cache-kit.module.ts | 16 ++++++---------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/adapters/in-memory-cache-store.adapter.ts b/src/adapters/in-memory-cache-store.adapter.ts index e78d36c..77cc5d9 100644 --- a/src/adapters/in-memory-cache-store.adapter.ts +++ b/src/adapters/in-memory-cache-store.adapter.ts @@ -95,9 +95,7 @@ export class InMemoryCacheStore implements ICacheStore { // Multiply seconds by 1 000 to convert to milliseconds for Date.now() comparison. // null signals "no expiry" so the entry lives until deleted or clear() is called. const expiresAt = - ttlSeconds !== undefined && ttlSeconds > 0 - ? Date.now() + ttlSeconds * 1_000 - : null; + ttlSeconds !== undefined && ttlSeconds > 0 ? Date.now() + ttlSeconds * 1_000 : null; // Serialize the value to a JSON string before storing to match Redis adapter behaviour this.store.set(key, { diff --git a/src/adapters/redis-cache-store.adapter.ts b/src/adapters/redis-cache-store.adapter.ts index f884015..712071c 100644 --- a/src/adapters/redis-cache-store.adapter.ts +++ b/src/adapters/redis-cache-store.adapter.ts @@ -70,8 +70,7 @@ export class RedisCacheStore implements ICacheStore { constructor(options: RedisCacheStoreOptions) { // Accept either an existing ioredis client or a plain connection URL string. // When a URL is provided we create a new dedicated client instance. - this.redis = - typeof options.client === "string" ? new Redis(options.client) : options.client; + this.redis = typeof options.client === "string" ? new Redis(options.client) : options.client; // Fall back to an empty string so buildKey() can skip the prefix logic. this.keyPrefix = options.keyPrefix ?? ""; diff --git a/src/cache-kit.module.ts b/src/cache-kit.module.ts index f49472c..804f371 100644 --- a/src/cache-kit.module.ts +++ b/src/cache-kit.module.ts @@ -59,9 +59,7 @@ export interface CacheModuleOptions { * Factory function type used by registerAsync's useFactory. * May return the options synchronously or as a Promise. */ -export type CacheModuleOptionsFactory = () => - | Promise - | CacheModuleOptions; +export type CacheModuleOptionsFactory = () => Promise | CacheModuleOptions; /** * Asynchronous configuration options for CacheModule.registerAsync(). @@ -111,7 +109,7 @@ function createStoreFromOptions(options: CacheModuleOptions): ICacheStore { if (!options.redis) { throw new Error( '[CacheModule] store is "redis" but no redis options were provided. ' + - 'Pass a `redis` field to CacheModule.register() or CacheModule.registerAsync().', + "Pass a `redis` field to CacheModule.register() or CacheModule.registerAsync().", ); } // Delegate all Redis connection and key-prefix logic to the adapter @@ -163,8 +161,9 @@ function createAsyncProviders(options: CacheModuleAsyncOptions): Provider[] { { // Call createCacheOptions() on the factory instance to get the options provide: CACHE_MODULE_OPTIONS, - useFactory: (factory: { createCacheOptions(): Promise | CacheModuleOptions }) => - factory.createCacheOptions(), + useFactory: (factory: { + createCacheOptions(): Promise | CacheModuleOptions; + }) => factory.createCacheOptions(), inject: [factoryClass], }, { @@ -252,10 +251,7 @@ export class CacheModule { module: CacheModule, // Import any modules required by the factory (e.g. ConfigModule) imports: options.imports ?? [], - providers: [ - ...asyncProviders, - CacheService, - ], + providers: [...asyncProviders, CacheService], exports: [CacheService, CACHE_STORE], }; } From d9e22e9c70abfc1d65610b577dc529c4f9229f99 Mon Sep 17 00:00:00 2001 From: yasser Date: Wed, 1 Apr 2026 09:55:44 +0100 Subject: [PATCH 4/8] style: fix Prettier formatting after develop merge --- src/adapters/in-memory-cache-store.adapter.ts | 4 +--- src/adapters/redis-cache-store.adapter.ts | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/adapters/in-memory-cache-store.adapter.ts b/src/adapters/in-memory-cache-store.adapter.ts index e78d36c..77cc5d9 100644 --- a/src/adapters/in-memory-cache-store.adapter.ts +++ b/src/adapters/in-memory-cache-store.adapter.ts @@ -95,9 +95,7 @@ export class InMemoryCacheStore implements ICacheStore { // Multiply seconds by 1 000 to convert to milliseconds for Date.now() comparison. // null signals "no expiry" so the entry lives until deleted or clear() is called. const expiresAt = - ttlSeconds !== undefined && ttlSeconds > 0 - ? Date.now() + ttlSeconds * 1_000 - : null; + ttlSeconds !== undefined && ttlSeconds > 0 ? Date.now() + ttlSeconds * 1_000 : null; // Serialize the value to a JSON string before storing to match Redis adapter behaviour this.store.set(key, { diff --git a/src/adapters/redis-cache-store.adapter.ts b/src/adapters/redis-cache-store.adapter.ts index f884015..712071c 100644 --- a/src/adapters/redis-cache-store.adapter.ts +++ b/src/adapters/redis-cache-store.adapter.ts @@ -70,8 +70,7 @@ export class RedisCacheStore implements ICacheStore { constructor(options: RedisCacheStoreOptions) { // Accept either an existing ioredis client or a plain connection URL string. // When a URL is provided we create a new dedicated client instance. - this.redis = - typeof options.client === "string" ? new Redis(options.client) : options.client; + this.redis = typeof options.client === "string" ? new Redis(options.client) : options.client; // Fall back to an empty string so buildKey() can skip the prefix logic. this.keyPrefix = options.keyPrefix ?? ""; From e15cb01bf4a3ac63541700ce865e9b398199917a Mon Sep 17 00:00:00 2001 From: yasser Date: Wed, 1 Apr 2026 10:08:26 +0100 Subject: [PATCH 5/8] fix(lint): fix import order and replace any types with proper NestJS types --- src/adapters/redis-cache-store.adapter.ts | 3 +-- src/cache-kit.module.ts | 17 ++++++++++++----- src/services/cache.service.ts | 1 - 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/adapters/redis-cache-store.adapter.ts b/src/adapters/redis-cache-store.adapter.ts index 712071c..36fb2c6 100644 --- a/src/adapters/redis-cache-store.adapter.ts +++ b/src/adapters/redis-cache-store.adapter.ts @@ -16,9 +16,8 @@ * - RedisCacheStore → the concrete Redis adapter class */ -import Redis from "ioredis"; - import type { ICacheStore } from "@ports/cache-store.port"; +import Redis from "ioredis"; // --------------------------------------------------------------------------- // Configuration diff --git a/src/cache-kit.module.ts b/src/cache-kit.module.ts index 804f371..c1fec22 100644 --- a/src/cache-kit.module.ts +++ b/src/cache-kit.module.ts @@ -16,11 +16,18 @@ * - CacheModule → the NestJS dynamic module class */ -import { DynamicModule, Module, Provider, Type } from "@nestjs/common"; - import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; import { RedisCacheStore } from "@adapters/redis-cache-store.adapter"; import type { RedisCacheStoreOptions } from "@adapters/redis-cache-store.adapter"; +import { + DynamicModule, + type InjectionToken, + Module, + type ModuleMetadata, + type OptionalFactoryDependency, + Provider, + Type, +} from "@nestjs/common"; import type { ICacheStore } from "@ports/cache-store.port"; import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "./constants"; @@ -70,10 +77,10 @@ export type CacheModuleOptionsFactory = () => Promise | Cach */ export interface CacheModuleAsyncOptions { /** Providers whose tokens are passed as arguments to useFactory. */ - inject?: any[]; + inject?: Array; /** Inline factory that resolves to CacheModuleOptions. */ - useFactory?: (...args: any[]) => Promise | CacheModuleOptions; + useFactory?: (...args: unknown[]) => Promise | CacheModuleOptions; /** * Class that the module will instantiate to obtain the options. @@ -88,7 +95,7 @@ export interface CacheModuleAsyncOptions { useExisting?: Type<{ createCacheOptions(): Promise | CacheModuleOptions }>; /** Additional NestJS modules to import into the async provider scope. */ - imports?: any[]; + imports?: ModuleMetadata["imports"]; } // --------------------------------------------------------------------------- diff --git a/src/services/cache.service.ts b/src/services/cache.service.ts index 3157e27..1927827 100644 --- a/src/services/cache.service.ts +++ b/src/services/cache.service.ts @@ -13,7 +13,6 @@ */ import { Inject, Injectable } from "@nestjs/common"; - import type { ICacheStore } from "@ports/cache-store.port"; import type { CacheModuleOptions } from "../cache-kit.module"; From 5886a6722ab0b35c3fab5134af42a5c94b4ae98d Mon Sep 17 00:00:00 2001 From: yasser Date: Thu, 2 Apr 2026 09:33:38 +0100 Subject: [PATCH 6/8] feat(COMPT-57): add @Cacheable and @CacheEvict decorators with key interpolation --- src/cache-kit.module.ts | 26 +++++++- src/decorators/cache-evict.decorator.ts | 76 ++++++++++++++++++++++ src/decorators/cacheable.decorator.ts | 85 +++++++++++++++++++++++++ src/index.ts | 11 ++++ src/utils/cache-service-ref.ts | 72 +++++++++++++++++++++ src/utils/resolve-cache-key.util.ts | 40 ++++++++++++ tsconfig.json | 3 +- 7 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 src/decorators/cache-evict.decorator.ts create mode 100644 src/decorators/cacheable.decorator.ts create mode 100644 src/utils/cache-service-ref.ts create mode 100644 src/utils/resolve-cache-key.util.ts diff --git a/src/cache-kit.module.ts b/src/cache-kit.module.ts index c1fec22..ec1935f 100644 --- a/src/cache-kit.module.ts +++ b/src/cache-kit.module.ts @@ -24,6 +24,8 @@ import { type InjectionToken, Module, type ModuleMetadata, + OnModuleInit, + Optional, type OptionalFactoryDependency, Provider, Type, @@ -32,6 +34,7 @@ import type { ICacheStore } from "@ports/cache-store.port"; import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "./constants"; import { CacheService } from "./services/cache.service"; +import { CacheServiceRef } from "./utils/cache-service-ref"; // --------------------------------------------------------------------------- // Configuration interfaces @@ -210,7 +213,28 @@ function createAsyncProviders(options: CacheModuleAsyncOptions): Provider[] { * ``` */ @Module({}) -export class CacheModule { +export class CacheModule implements OnModuleInit { + constructor( + /** + * Injected by NestJS from the providers registered in register() / registerAsync(). + * @Optional() guards against the rare case where the module class is instantiated + * without CacheService being available (e.g. partial test setups). + */ + @Optional() private readonly cacheService?: CacheService, + ) {} + + /** + * Runs after all providers in this module have been resolved. + * Stores the CacheService reference so @Cacheable and @CacheEvict can access + * it at method-call time without going through constructor injection. + */ + onModuleInit(): void { + // Only populate the ref when CacheService is available + if (this.cacheService) { + CacheServiceRef.set(this.cacheService); + } + } + /** * Register the module with synchronous, inline configuration. * diff --git a/src/decorators/cache-evict.decorator.ts b/src/decorators/cache-evict.decorator.ts new file mode 100644 index 0000000..dc2057c --- /dev/null +++ b/src/decorators/cache-evict.decorator.ts @@ -0,0 +1,76 @@ +/** + * @file cache-evict.decorator.ts + * + * @CacheEvict method decorator — removes a cache entry after the method executes. + * + * When applied to a method, it: + * 1. Calls the original method and awaits its result. + * 2. Resolves the cache key by interpolating method arguments into the template. + * 3. Deletes the matching cache entry so the next read fetches fresh data. + * + * The eviction happens AFTER the method succeeds — if the method throws, the + * cache entry is left intact. + * + * Works with both sync and async methods. The wrapped method always returns a + * Promise because the cache delete operation is asynchronous. + * + * CacheService is resolved from the singleton CacheServiceRef which is populated + * by CacheModule.onModuleInit() — no extra injection is required in consumer classes. + * + * Exports: + * - CacheEvict → method decorator factory + */ + +import { CacheServiceRef } from "@utils/cache-service-ref"; +import { resolveCacheKey } from "@utils/resolve-cache-key.util"; + +/** + * Cache eviction method decorator. + * + * @param key - Cache key template to delete after the method executes. + * Use `{0}`, `{1}`, … to interpolate method arguments, + * e.g. `"user:{0}"`. + * + * @example Static key eviction + * ```typescript + * @CacheEvict("all-products") + * async createProduct(dto: CreateProductDto): Promise { ... } + * ``` + * + * @example Dynamic key with argument interpolation + * ```typescript + * @CacheEvict("user:{0}") + * async updateUser(id: string, dto: UpdateUserDto): Promise { ... } + * ``` + */ +export function CacheEvict(key: string): MethodDecorator { + return ( + _target: object, + _propertyKey: string | symbol, + descriptor: PropertyDescriptor, + ): PropertyDescriptor => { + // Capture the original method before replacing it + const originalMethod = descriptor.value as (...args: unknown[]) => unknown; + + // Replace the method with a cache-evicting wrapper + descriptor.value = async function (this: unknown, ...args: unknown[]): Promise { + // Resolve the CacheService from the module-level singleton + const cacheService = CacheServiceRef.get(); + + // ── Execute the original method first ────────────────────────────── + // Wrap in Promise.resolve() to support both sync and async methods. + // If the method throws, the error propagates and eviction is skipped + // so we don't invalidate a cache entry for a failed operation. + const result = await Promise.resolve(originalMethod.apply(this, args)); + + // ── Evict cache entry after successful execution ─────────────────── + // Interpolate {n} placeholders using the actual call arguments + const resolvedKey = resolveCacheKey(key, args); + await cacheService.delete(resolvedKey); + + return result; + }; + + return descriptor; + }; +} diff --git a/src/decorators/cacheable.decorator.ts b/src/decorators/cacheable.decorator.ts new file mode 100644 index 0000000..16a7839 --- /dev/null +++ b/src/decorators/cacheable.decorator.ts @@ -0,0 +1,85 @@ +/** + * @file cacheable.decorator.ts + * + * @Cacheable method decorator — implements the cache-aside pattern automatically. + * + * When applied to a method, it: + * 1. Resolves the cache key by interpolating method arguments into the template. + * 2. Returns the cached value immediately if it exists (cache hit). + * 3. On a cache miss, calls the original method, stores the result, and returns it. + * + * Works with both sync and async methods. The wrapped method always returns a Promise + * because cache read/write operations are inherently asynchronous. + * + * CacheService is resolved from the singleton CacheServiceRef which is populated + * by CacheModule.onModuleInit() — no extra injection is required in consumer classes. + * + * Exports: + * - Cacheable → method decorator factory + */ + +import { CacheServiceRef } from "@utils/cache-service-ref"; +import { resolveCacheKey } from "@utils/resolve-cache-key.util"; + +/** + * Cache-aside method decorator. + * + * @param key - Cache key template. Use `{0}`, `{1}`, … to interpolate + * method arguments, e.g. `"user:{0}"`. + * @param ttlSeconds - Optional TTL in seconds. Falls back to the module-level + * default TTL configured in CacheModule.register(). + * + * @example Static key + * ```typescript + * @Cacheable("all-products", 300) + * async findAllProducts(): Promise { ... } + * ``` + * + * @example Dynamic key with argument interpolation + * ```typescript + * @Cacheable("user:{0}", 60) + * async findUserById(id: string): Promise { ... } + * ``` + * + * @example Without explicit TTL (inherits module default) + * ```typescript + * @Cacheable("config") + * async getConfig(): Promise { ... } + * ``` + */ +export function Cacheable(key: string, ttlSeconds?: number): MethodDecorator { + return ( + _target: object, + _propertyKey: string | symbol, + descriptor: PropertyDescriptor, + ): PropertyDescriptor => { + // Capture the original method before replacing it + const originalMethod = descriptor.value as (...args: unknown[]) => unknown; + + // Replace the method with a cache-aware wrapper + descriptor.value = async function (this: unknown, ...args: unknown[]): Promise { + // Resolve the CacheService from the module-level singleton + const cacheService = CacheServiceRef.get(); + + // Interpolate any {n} placeholders in the key template with the actual args + const resolvedKey = resolveCacheKey(key, args); + + // ── Cache hit ────────────────────────────────────────────────────── + // Return the stored value immediately without calling the original method + const cached = await cacheService.get(resolvedKey); + if (cached !== null) return cached; + + // ── Cache miss ───────────────────────────────────────────────────── + // Call the original method; wrap in Promise.resolve() to handle both + // sync methods (returns a plain value) and async methods (returns a Promise) + const result = await Promise.resolve(originalMethod.apply(this, args)); + + // Persist the result under the resolved key for future calls + await cacheService.set(resolvedKey, result, ttlSeconds); + + return result; + }; + + return descriptor; + }; +} diff --git a/src/index.ts b/src/index.ts index 3ff9c23..4755cd6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,17 @@ export { ExampleGuard } from "./guards/example.guard"; // Export decorators for use in consumer controllers/services export { ExampleData, ExampleParam } from "./decorators/example.decorator"; +// ============================================================================ +// DECORATORS +// ============================================================================ +// Method decorators for automatic caching and cache invalidation. +// Apply these to service methods — no manual CacheService injection needed. + +// Cache-aside decorator: returns cached value or calls the method and stores the result +export { Cacheable } from "./decorators/cacheable.decorator"; +// Cache eviction decorator: deletes the cache entry after the method executes +export { CacheEvict } from "./decorators/cache-evict.decorator"; + // ============================================================================ // TYPES & INTERFACES (For TypeScript Typing) // ============================================================================ diff --git a/src/utils/cache-service-ref.ts b/src/utils/cache-service-ref.ts new file mode 100644 index 0000000..13ce14f --- /dev/null +++ b/src/utils/cache-service-ref.ts @@ -0,0 +1,72 @@ +/** + * @file cache-service-ref.ts + * + * Module-scoped singleton reference to the active CacheService instance. + * + * Why this exists: + * Method decorators (@Cacheable, @CacheEvict) are applied at class-definition + * time, long before NestJS has assembled the DI container. They cannot receive + * CacheService via constructor injection. Instead, CacheModule stores a reference + * here during `onModuleInit()`, and the decorators read it at call time. + * + * Lifecycle: + * 1. App bootstraps → NestJS initialises CacheModule. + * 2. CacheModule.onModuleInit() calls CacheServiceRef.set(cacheService). + * 3. First method call with @Cacheable / @CacheEvict → CacheServiceRef.get() succeeds. + * + * Exports: + * - CacheServiceRef → { set, get } singleton accessor + */ + +import type { CacheService } from "@services/cache.service"; + +// --------------------------------------------------------------------------- +// Internal holder — private to this module +// --------------------------------------------------------------------------- + +/** + * The single shared CacheService instance. + * null until CacheModule.onModuleInit() runs. + */ +let _instance: CacheService | null = null; + +// --------------------------------------------------------------------------- +// Public accessor +// --------------------------------------------------------------------------- + +/** + * Singleton accessor for the active CacheService. + * + * Populated by CacheModule during application bootstrap. + * Used internally by @Cacheable and @CacheEvict at method-call time. + */ +export const CacheServiceRef = { + /** + * Store the CacheService instance. + * Called once by CacheModule.onModuleInit(). + * + * @param service - The resolved CacheService from NestJS DI + */ + set(service: CacheService): void { + // Overwrite any prior value — safe for hot-reload scenarios + _instance = service; + }, + + /** + * Retrieve the stored CacheService. + * Throws a descriptive error if called before the module has initialised. + * + * @returns The active CacheService instance + * @throws {Error} If CacheModule was not imported in the application + */ + get(): CacheService { + if (_instance === null) { + throw new Error( + "[CacheKit] CacheService is not initialised. " + + "Make sure CacheModule.register() or CacheModule.registerAsync() " + + "is imported in your root AppModule before using @Cacheable or @CacheEvict.", + ); + } + return _instance; + }, +}; diff --git a/src/utils/resolve-cache-key.util.ts b/src/utils/resolve-cache-key.util.ts new file mode 100644 index 0000000..6b0f641 --- /dev/null +++ b/src/utils/resolve-cache-key.util.ts @@ -0,0 +1,40 @@ +/** + * @file resolve-cache-key.util.ts + * + * Utility for resolving cache key templates at runtime. + * + * Templates support positional argument interpolation using the `{n}` syntax, + * where `n` is the zero-based index of the method argument. + * + * Exports: + * - resolveCacheKey → replaces `{0}`, `{1}`, … in a template with actual argument values + */ + +/** + * Resolve a cache key template by substituting `{n}` placeholders with the + * corresponding method argument values. + * + * Rules: + * - `{0}` is replaced with `String(args[0])` + * - `{1}` is replaced with `String(args[1])`, and so on + * - Placeholders that reference a missing argument are replaced with an empty string + * + * @param template - Key template, e.g. `"user:{0}"` or `"post:{0}:comment:{1}"` + * @param args - The method arguments passed at call time + * @returns Fully resolved cache key string + * + * @example + * resolveCacheKey("user:{0}", ["42"]) // → "user:42" + * resolveCacheKey("post:{0}:comments", [7]) // → "post:7:comments" + * resolveCacheKey("static-key", []) // → "static-key" + */ +export function resolveCacheKey(template: string, args: unknown[]): string { + // Replace every {n} token with the stringified value of args[n]. + // The regex matches literal braces wrapping one or more digits. + return template.replace(/\{(\d+)\}/g, (_match, indexStr: string) => { + const value = args[Number(indexStr)]; + + // If the argument exists, coerce it to a string; otherwise leave empty + return value !== undefined && value !== null ? String(value) : ""; + }); +} diff --git a/tsconfig.json b/tsconfig.json index 2010ba7..a7200f8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -32,7 +32,8 @@ "@middleware/*": ["src/middleware/*"], "@utils/*": ["src/utils/*"], "@ports/*": ["src/ports/*"], - "@adapters/*": ["src/adapters/*"] + "@adapters/*": ["src/adapters/*"], + "@utils/*": ["src/utils/*"] } }, "include": ["src/**/*.ts", "test/**/*.ts"], From c5dbd190625a3f8a521cfce04b27df7c8a466894 Mon Sep 17 00:00:00 2001 From: yasser Date: Fri, 3 Apr 2026 11:45:50 +0100 Subject: [PATCH 7/8] test(COMPT-58): add full test suite with 95%+ coverage across all adapters, service, and decorators --- jest.config.ts | 10 +- package-lock.json | 89 ++++++++ package.json | 1 + .../in-memory-cache-store.adapter.spec.ts | 182 ++++++++++++++++ .../redis-cache-store.adapter.spec.ts | 191 ++++++++++++++++ src/cache-kit.module.spec.ts | 116 ++++++++++ src/decorators/cache-evict.decorator.spec.ts | 160 ++++++++++++++ src/decorators/cacheable.decorator.spec.ts | 175 +++++++++++++++ src/services/cache.service.spec.ts | 203 ++++++++++++++++++ src/utils/cache-service-ref.spec.ts | 71 ++++++ src/utils/resolve-cache-key.util.spec.ts | 91 ++++++++ 11 files changed, 1285 insertions(+), 4 deletions(-) create mode 100644 src/adapters/in-memory-cache-store.adapter.spec.ts create mode 100644 src/adapters/redis-cache-store.adapter.spec.ts create mode 100644 src/cache-kit.module.spec.ts create mode 100644 src/decorators/cache-evict.decorator.spec.ts create mode 100644 src/decorators/cacheable.decorator.spec.ts create mode 100644 src/services/cache.service.spec.ts create mode 100644 src/utils/cache-service-ref.spec.ts create mode 100644 src/utils/resolve-cache-key.util.spec.ts diff --git a/jest.config.ts b/jest.config.ts index 958fd0b..eda9ad8 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -3,6 +3,8 @@ import type { Config } from "jest"; const config: Config = { testEnvironment: "node", clearMocks: true, + // forceExit closes open ioredis handles left by ioredis-mock after tests finish + forceExit: true, testMatch: ["/test/**/*.test.ts", "/src/**/*.spec.ts"], transform: { "^.+\\.ts$": ["ts-jest", { tsconfig: "tsconfig.json" }], @@ -27,10 +29,10 @@ const config: Config = { coverageDirectory: "coverage", coverageThreshold: { global: { - branches: 80, - functions: 80, - lines: 80, - statements: 80, + branches: 85, + functions: 85, + lines: 85, + statements: 85, }, }, moduleNameMapper: { diff --git a/package-lock.json b/package-lock.json index cc2bc3d..3017e1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "eslint-plugin-import": "^2.32.0", "globals": "^16.5.0", "husky": "^9.1.7", + "ioredis-mock": "^8.13.1", "jest": "^29.7.0", "lint-staged": "^16.2.7", "prettier": "^3.4.2", @@ -1363,6 +1364,13 @@ } } }, + "node_modules/@ioredis/as-callback": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@ioredis/as-callback/-/as-callback-3.0.0.tgz", + "integrity": "sha512-Kqv1rZ3WbgOrS+hgzJ5xG5WQuhvzzSTRYvNeyPMLOAM78MHSnuKI20JeJGbpuAt//LCuP0vsexZcorqW7kWhJg==", + "dev": true, + "license": "MIT" + }, "node_modules/@ioredis/commands": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.5.1.tgz", @@ -2625,6 +2633,17 @@ "@types/node": "*" } }, + "node_modules/@types/ioredis-mock": { + "version": "8.2.7", + "resolved": "https://registry.npmjs.org/@types/ioredis-mock/-/ioredis-mock-8.2.7.tgz", + "integrity": "sha512-YsGiaOIYBKeVvu/7GYziAD8qX3LJem5LK00d5PKykzsQJMLysAqXA61AkNuYWCekYl64tbMTqVOMF4SYoCPbQg==", + "dev": true, + "license": "MIT", + "peer": true, + "peerDependencies": { + "ioredis": ">=5" + } + }, "node_modules/@types/istanbul-lib-coverage": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz", @@ -5164,6 +5183,35 @@ } } }, + "node_modules/fengari": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/fengari/-/fengari-0.1.5.tgz", + "integrity": "sha512-0DS4Nn4rV8qyFlQCpKK8brT61EUtswynrpfFTcgLErcilBIBskSMQ86fO2WVuybr14ywyKdRjv91FiRZwnEuvQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "readline-sync": "^1.4.10", + "sprintf-js": "^1.1.3", + "tmp": "^0.2.5" + } + }, + "node_modules/fengari-interop": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/fengari-interop/-/fengari-interop-0.1.4.tgz", + "integrity": "sha512-4/CW/3PJUo3ebD4ACgE1g/3NGEYSq7OQAyETyypsAl/WeySDBbxExikkayNkZzbpgyC9GyJp8v1DU2VOXxNq7Q==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "fengari": "^0.1.0" + } + }, + "node_modules/fengari/node_modules/sprintf-js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", + "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==", + "dev": true, + "license": "BSD-3-Clause" + }, "node_modules/fflate": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", @@ -5952,6 +6000,27 @@ "url": "https://opencollective.com/ioredis" } }, + "node_modules/ioredis-mock": { + "version": "8.13.1", + "resolved": "https://registry.npmjs.org/ioredis-mock/-/ioredis-mock-8.13.1.tgz", + "integrity": "sha512-Wsi50AU+cMiI32nAgfwpUaJVBtb4iQdVsOHl9M6R3tePCO/8vGsToCVIG82XWAxN4Se55TZoOzVseu+QngFLyw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ioredis/as-callback": "^3.0.0", + "@ioredis/commands": "^1.4.0", + "fengari": "^0.1.4", + "fengari-interop": "^0.1.3", + "semver": "^7.7.2" + }, + "engines": { + "node": ">=12.22" + }, + "peerDependencies": { + "@types/ioredis-mock": "^8", + "ioredis": "^5" + } + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -8651,6 +8720,16 @@ "node": ">= 6" } }, + "node_modules/readline-sync": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/redis-errors": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", @@ -9634,6 +9713,16 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/tmp": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.5.tgz", + "integrity": "sha512-voyz6MApa1rQGUxT3E+BK7/ROe8itEx7vD8/HEvt4xwXucvQ5G5oeEiHkmHZJuBO21RpOf+YYm9MOivj709jow==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.14" + } + }, "node_modules/tmpl": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", diff --git a/package.json b/package.json index fd7b393..aba7a47 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "eslint-plugin-import": "^2.32.0", "globals": "^16.5.0", "husky": "^9.1.7", + "ioredis-mock": "^8.13.1", "jest": "^29.7.0", "lint-staged": "^16.2.7", "prettier": "^3.4.2", diff --git a/src/adapters/in-memory-cache-store.adapter.spec.ts b/src/adapters/in-memory-cache-store.adapter.spec.ts new file mode 100644 index 0000000..53afbcf --- /dev/null +++ b/src/adapters/in-memory-cache-store.adapter.spec.ts @@ -0,0 +1,182 @@ +/** + * @file in-memory-cache-store.adapter.spec.ts + * + * Unit tests for InMemoryCacheStore — the Map-backed ICacheStore adapter. + * + * Tests cover: + * - Full ICacheStore contract: get, set, delete, clear + * - TTL expiry: entries expire after TTL elapses and are present before + * - Parse-error resilience: malformed JSON stored directly returns null + * - No-expiry behaviour: entries without TTL persist until explicitly cleared + */ + +import { InMemoryCacheStore } from "./in-memory-cache-store.adapter"; + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +/** Advance Date.now() by `ms` milliseconds inside a test block */ +function advanceTimeBy(ms: number): void { + jest.spyOn(Date, "now").mockReturnValue(Date.now() + ms); +} + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("InMemoryCacheStore", () => { + let store: InMemoryCacheStore; + + // Create a fresh, empty store before every test so state never leaks + beforeEach(() => { + store = new InMemoryCacheStore(); + jest.restoreAllMocks(); // reset Date.now() spy between tests + }); + + // ── get ────────────────────────────────────────────────────────────────── + + describe("get()", () => { + it("returns null for a key that was never set", async () => { + // Querying an empty store must return a cache miss + const result = await store.get("missing"); + expect(result).toBeNull(); + }); + + it("returns the stored value on a cache hit", async () => { + // Store a plain object, then retrieve it + await store.set("key", { name: "Alice" }); + const result = await store.get<{ name: string }>("key"); + expect(result).toEqual({ name: "Alice" }); + }); + + it("returns null for a key that has been deleted", async () => { + // Set then immediately delete — get() must return null + await store.set("key", "value"); + await store.delete("key"); + expect(await store.get("key")).toBeNull(); + }); + + it("returns null when stored JSON is malformed (parse error → null)", async () => { + // Bypass public API and inject invalid JSON directly into the backing Map + // to test the try/catch parse-error path + const raw = (store as unknown as { store: Map }) + .store; + raw.set("bad", { value: "not-valid-json{{", expiresAt: null }); + + const result = await store.get("bad"); + expect(result).toBeNull(); + }); + }); + + // ── set ────────────────────────────────────────────────────────────────── + + describe("set()", () => { + it("overwrites an existing value for the same key", async () => { + // Second set() must replace the first + await store.set("key", "first"); + await store.set("key", "second"); + expect(await store.get("key")).toBe("second"); + }); + + it("stores primitive, array, and object values correctly", async () => { + // Validates JSON round-trip for different value shapes + await store.set("num", 42); + await store.set("arr", [1, 2, 3]); + await store.set("obj", { a: 1 }); + + expect(await store.get("num")).toBe(42); + expect(await store.get("arr")).toEqual([1, 2, 3]); + expect(await store.get("obj")).toEqual({ a: 1 }); + }); + + it("entry without TTL persists indefinitely", async () => { + // No TTL means the entry should survive any time advance + await store.set("persistent", "value"); + advanceTimeBy(999_999_000); // advance ~11.5 days + expect(await store.get("persistent")).toBe("value"); + }); + + it("entry with TTL=0 is treated as no expiry", async () => { + // TTL of 0 is the same as omitting the TTL + await store.set("key", "value", 0); + advanceTimeBy(5_000); // advance 5 seconds + expect(await store.get("key")).toBe("value"); + }); + }); + + // ── TTL ────────────────────────────────────────────────────────────────── + + describe("TTL expiry", () => { + it("entry is present before TTL elapses", async () => { + // Entry set with 10-second TTL must be readable immediately + await store.set("ttl-key", "alive", 10); + expect(await store.get("ttl-key")).toBe("alive"); + }); + + it("entry expires and returns null after TTL elapses", async () => { + // Set with 5-second TTL, then advance time past the deadline + await store.set("ttl-key", "bye", 5); + advanceTimeBy(6_000); // 6 s > 5 s TTL + expect(await store.get("ttl-key")).toBeNull(); + }); + + it("expired entry is removed from the store on access (lazy eviction)", async () => { + // After expiry + access, the backing Map must no longer hold the entry + await store.set("ttl-key", "stale", 1); + advanceTimeBy(2_000); + await store.get("ttl-key"); // triggers lazy delete + + const raw = (store as unknown as { store: Map }).store; + expect(raw.has("ttl-key")).toBe(false); + }); + }); + + // ── delete ─────────────────────────────────────────────────────────────── + + describe("delete()", () => { + it("removes an existing entry", async () => { + await store.set("key", "value"); + await store.delete("key"); + expect(await store.get("key")).toBeNull(); + }); + + it("is a no-op when the key does not exist (does not throw)", async () => { + // Deleting a missing key must succeed silently + await expect(store.delete("ghost")).resolves.toBeUndefined(); + }); + }); + + // ── clear ───────────────────────────────────────────────────────────────── + + describe("clear()", () => { + it("removes all entries from the store", async () => { + // Populate with several entries then clear + await store.set("a", 1); + await store.set("b", 2); + await store.set("c", 3); + + await store.clear(); + + // All three keys must be gone + expect(await store.get("a")).toBeNull(); + expect(await store.get("b")).toBeNull(); + expect(await store.get("c")).toBeNull(); + }); + + it("is safe to call on an empty store", async () => { + // Clearing an empty store must not throw + await expect(store.clear()).resolves.toBeUndefined(); + }); + }); + + // ── has (via CacheService — tested through adapter directly) ───────────── + + describe("get() after clear()", () => { + it("returns null for a key that existed before clear()", async () => { + await store.set("key", "value"); + await store.clear(); + expect(await store.get("key")).toBeNull(); + }); + }); +}); diff --git a/src/adapters/redis-cache-store.adapter.spec.ts b/src/adapters/redis-cache-store.adapter.spec.ts new file mode 100644 index 0000000..35a7732 --- /dev/null +++ b/src/adapters/redis-cache-store.adapter.spec.ts @@ -0,0 +1,191 @@ +/** + * @file redis-cache-store.adapter.spec.ts + * + * Unit tests for RedisCacheStore — the ioredis-backed ICacheStore adapter. + * + * Uses ioredis-mock so no real Redis server is required. The mock exposes the + * same API surface as real ioredis, meaning all RedisCacheStore code paths + * (get, set+EX, del, keys+del, flushdb) are exercised against in-memory state. + * + * Tests cover: + * - Full ICacheStore contract: get, set (with and without TTL), delete, clear + * - Key prefix namespacing: keys are stored and retrieved with the prefix + * - clear() with prefix: only prefixed keys are removed + * - clear() without prefix: full flushdb is called + * - Parse-error resilience: malformed JSON stored in Redis returns null + * - Constructor accepts both a URL string and an existing Redis instance + */ + +// ioredis-mock is a drop-in in-memory replacement for ioredis used during tests +import RedisMock from "ioredis-mock"; + +import { RedisCacheStore } from "./redis-cache-store.adapter"; + +// Derive the instance type from the constructor to avoid the namespace-as-type error +type RedisMockInstance = InstanceType; + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("RedisCacheStore", () => { + // ── Without key prefix ─────────────────────────────────────────────────── + + describe("without keyPrefix", () => { + let redis: RedisMockInstance; + let store: RedisCacheStore; + + beforeEach(() => { + // Create a fresh mock client before every test to avoid state leaking + redis = new RedisMock(); + // Pass the mock client directly (exercises the "existing client" code path) + store = new RedisCacheStore({ client: redis as never }); + }); + + // ── get ───────────────────────────────────────────────────────────── + + describe("get()", () => { + it("returns null for a key that does not exist", async () => { + expect(await store.get("missing")).toBeNull(); + }); + + it("returns the deserialized value on a cache hit", async () => { + // Pre-populate the mock Redis directly with a serialized value + await redis.set("key", JSON.stringify({ id: 1 })); + const result = await store.get<{ id: number }>("key"); + expect(result).toEqual({ id: 1 }); + }); + + it("returns null when the stored value is malformed JSON", async () => { + // Store invalid JSON directly in the mock — RedisCacheStore must return null + await redis.set("bad", "not-json{{"); + expect(await store.get("bad")).toBeNull(); + }); + }); + + // ── set ───────────────────────────────────────────────────────────── + + describe("set()", () => { + it("stores a value without TTL and it persists", async () => { + await store.set("key", { name: "Bob" }); + // Verify the raw string is present in the mock + const raw = await redis.get("key"); + expect(JSON.parse(raw!)).toEqual({ name: "Bob" }); + }); + + it("stores a value with TTL using the EX flag", async () => { + // Spy on the mock redis.set to confirm EX is passed + const setSpy = jest.spyOn(redis, "set"); + await store.set("key", "value", 30); + // EX and the TTL value must appear as arguments + expect(setSpy).toHaveBeenCalledWith("key", '"value"', "EX", 30); + }); + + it("stores a value without TTL when ttlSeconds is 0", async () => { + const setSpy = jest.spyOn(redis, "set"); + await store.set("key", "value", 0); + // Should call the two-argument overload (no EX) + expect(setSpy).toHaveBeenCalledWith("key", '"value"'); + }); + }); + + // ── delete ─────────────────────────────────────────────────────────── + + describe("delete()", () => { + it("removes an existing key", async () => { + await redis.set("key", JSON.stringify("hello")); + await store.delete("key"); + expect(await redis.get("key")).toBeNull(); + }); + + it("is a no-op when the key does not exist", async () => { + // Must not throw even when the key is absent + await expect(store.delete("ghost")).resolves.toBeUndefined(); + }); + }); + + // ── clear (no prefix → flushdb) ────────────────────────────────────── + + describe("clear() without prefix", () => { + it("calls flushdb and empties the entire Redis database", async () => { + const flushSpy = jest.spyOn(redis, "flushdb"); + await redis.set("a", "1"); + await redis.set("b", "2"); + + await store.clear(); + + // flushdb must have been called once + expect(flushSpy).toHaveBeenCalledTimes(1); + // Both keys must be gone from the mock + expect(await redis.get("a")).toBeNull(); + expect(await redis.get("b")).toBeNull(); + }); + }); + }); + + // ── With key prefix ────────────────────────────────────────────────────── + + describe("with keyPrefix", () => { + let redis: RedisMockInstance; + let store: RedisCacheStore; + const PREFIX = "app"; + + beforeEach(() => { + redis = new RedisMock(); + store = new RedisCacheStore({ client: redis as never, keyPrefix: PREFIX }); + }); + + it("prefixes every key on set and get", async () => { + // After set("user"), the raw Redis key must be "app:user" + await store.set("user", { id: 42 }); + const raw = await redis.get(`${PREFIX}:user`); + expect(JSON.parse(raw!)).toEqual({ id: 42 }); + }); + + it("get() resolves using the prefixed key", async () => { + // Pre-populate under the prefixed key, then get() without the prefix + await redis.set(`${PREFIX}:item`, JSON.stringify("cached")); + expect(await store.get("item")).toBe("cached"); + }); + + it("delete() removes the prefixed key", async () => { + await redis.set(`${PREFIX}:key`, JSON.stringify("v")); + await store.delete("key"); + expect(await redis.get(`${PREFIX}:key`)).toBeNull(); + }); + + // ── clear (with prefix → keys + del) ──────────────────────────────── + + describe("clear() with prefix", () => { + it("removes only keys matching the prefix pattern", async () => { + // Populate both prefixed and non-prefixed keys + await redis.set(`${PREFIX}:x`, "1"); + await redis.set(`${PREFIX}:y`, "2"); + await redis.set("other:z", "3"); // must NOT be deleted + + await store.clear(); + + // Prefixed keys are gone + expect(await redis.get(`${PREFIX}:x`)).toBeNull(); + expect(await redis.get(`${PREFIX}:y`)).toBeNull(); + // Non-prefixed key is untouched + expect(await redis.get("other:z")).toBe("3"); + }); + + it("does not call DEL when no prefixed keys exist", async () => { + const delSpy = jest.spyOn(redis, "del"); + await store.clear(); // nothing under "app:*" + expect(delSpy).not.toHaveBeenCalled(); + }); + }); + }); + + // ── URL string constructor ──────────────────────────────────────────────── + + describe("constructor with URL string", () => { + it("accepts a connection URL string (exercises the string branch)", () => { + // Passing a URL string must not throw — the constructor creates an internal client + expect(() => new RedisCacheStore({ client: "redis://localhost:6379" })).not.toThrow(); + }); + }); +}); diff --git a/src/cache-kit.module.spec.ts b/src/cache-kit.module.spec.ts new file mode 100644 index 0000000..7c42b92 --- /dev/null +++ b/src/cache-kit.module.spec.ts @@ -0,0 +1,116 @@ +/** + * @file cache-kit.module.spec.ts + * + * Unit tests for CacheModule — the NestJS dynamic module. + * + * Tests cover: + * - register() wires the InMemory adapter when store is "memory" + * - register() wires the Redis adapter when store is "redis" + * - register() throws when store is "redis" but no redis options are given + * - registerAsync() resolves options via useFactory and wires CacheService + * - onModuleInit() populates CacheServiceRef with the CacheService instance + */ + +import { Test } from "@nestjs/testing"; + +import { CacheServiceRef } from "@utils/cache-service-ref"; + +import { CacheModule } from "./cache-kit.module"; +import { CacheService } from "./services/cache.service"; + +describe("CacheModule", () => { + // ── register() — memory store ───────────────────────────────────────────── + + describe("register() with store: memory", () => { + it("provides CacheService and it is injectable", async () => { + // Compile a minimal NestJS module using the synchronous registration path + const module = await Test.createTestingModule({ + imports: [CacheModule.register({ store: "memory" })], + }).compile(); + + // CacheService must be resolvable from the module's DI container + const service = module.get(CacheService); + expect(service).toBeInstanceOf(CacheService); + }); + + it("CacheService.get() returns null for an unknown key (full integration)", async () => { + const module = await Test.createTestingModule({ + imports: [CacheModule.register({ store: "memory", ttl: 60 })], + }).compile(); + + const service = module.get(CacheService); + expect(await service.get("unknown")).toBeNull(); + }); + + it("exposes get/set/has/delete/clear/wrap through CacheService", async () => { + const module = await Test.createTestingModule({ + imports: [CacheModule.register({ store: "memory" })], + }).compile(); + + const service = module.get(CacheService); + // Basic round-trip: set then get + await service.set("key", "value"); + expect(await service.get("key")).toBe("value"); + expect(await service.has("key")).toBe(true); + await service.delete("key"); + expect(await service.get("key")).toBeNull(); + }); + }); + + // ── register() — redis store validation ────────────────────────────────── + + describe("register() with store: redis", () => { + it("throws when no redis options are provided", () => { + // The factory function is called at registration time for synchronous options + expect(() => CacheModule.register({ store: "redis" })).toThrow(/redis.*options/i); + }); + }); + + // ── registerAsync() ─────────────────────────────────────────────────────── + + describe("registerAsync() with useFactory", () => { + it("resolves options from the factory and provides CacheService", async () => { + const module = await Test.createTestingModule({ + imports: [ + CacheModule.registerAsync({ + // useFactory resolves synchronously here for simplicity + useFactory: () => ({ store: "memory" as const }), + }), + ], + }).compile(); + + const service = module.get(CacheService); + expect(service).toBeInstanceOf(CacheService); + }); + + it("resolves options from an async factory (Promise)", async () => { + const module = await Test.createTestingModule({ + imports: [ + CacheModule.registerAsync({ + useFactory: async () => ({ store: "memory" as const, ttl: 30 }), + }), + ], + }).compile(); + + const service = module.get(CacheService); + expect(service).toBeInstanceOf(CacheService); + }); + }); + + // ── onModuleInit() populates CacheServiceRef ────────────────────────────── + + describe("onModuleInit()", () => { + it("populates CacheServiceRef so @Cacheable / @CacheEvict can resolve the service", async () => { + const module = await Test.createTestingModule({ + imports: [CacheModule.register({ store: "memory" })], + }).compile(); + + // init() triggers onModuleInit on all providers + await module.init(); + + // CacheServiceRef.get() must succeed (not throw) after init + expect(() => CacheServiceRef.get()).not.toThrow(); + expect(CacheServiceRef.get()).toBeInstanceOf(CacheService); + }); + }); +}); diff --git a/src/decorators/cache-evict.decorator.spec.ts b/src/decorators/cache-evict.decorator.spec.ts new file mode 100644 index 0000000..27006a2 --- /dev/null +++ b/src/decorators/cache-evict.decorator.spec.ts @@ -0,0 +1,160 @@ +/** + * @file cache-evict.decorator.spec.ts + * + * Unit tests for the @CacheEvict method decorator. + * + * Uses a real InMemoryCacheStore and CacheService (via CacheServiceRef) so the + * complete eviction path is exercised end-to-end without mocking internals. + * + * Tests cover: + * - Cache entry is deleted after the method executes successfully + * - The method return value is preserved and returned to the caller + * - Cache entry is NOT deleted when the method throws (eviction is skipped) + * - Key template interpolation: "user:{0}" with arg "42" → entry "user:42" is evicted + * - Works on async methods + * - Works on sync methods + */ + +import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; +import { CacheServiceRef } from "@utils/cache-service-ref"; + +import type { CacheModuleOptions } from "../cache-kit.module"; +import { CacheService } from "../services/cache.service"; +import { CacheEvict } from "./cache-evict.decorator"; + +// --------------------------------------------------------------------------- +// Setup helpers +// --------------------------------------------------------------------------- + +/** Wire a real CacheService backed by a fresh InMemoryCacheStore into CacheServiceRef */ +function setupCacheServiceRef(): { service: CacheService; store: InMemoryCacheStore } { + const store = new InMemoryCacheStore(); + const options: CacheModuleOptions = { store: "memory" }; + const service = new (CacheService as new ( + store: InMemoryCacheStore, + options: CacheModuleOptions, + ) => CacheService)(store, options); + CacheServiceRef.set(service); + return { service, store }; +} + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("@CacheEvict decorator", () => { + let service: CacheService; + + beforeEach(() => { + ({ service } = setupCacheServiceRef()); + }); + + // ── Basic eviction ──────────────────────────────────────────────────────── + + it("deletes the specified cache entry after the method executes", async () => { + // Pre-populate the cache so we can verify it disappears + await service.set("product:1", { price: 99 }); + + class ProductService { + @CacheEvict("product:1") + async updateProduct() { + return { updated: true }; + } + } + + await new ProductService().updateProduct(); + + // The entry must be gone after the decorated method ran + expect(await service.get("product:1")).toBeNull(); + }); + + it("returns the original method's return value unchanged", async () => { + class OrderService { + @CacheEvict("order:1") + async createOrder() { + return { orderId: "abc" }; + } + } + + const result = await new OrderService().createOrder(); + // The decorator must not modify the return value + expect(result).toEqual({ orderId: "abc" }); + }); + + // ── Error path ──────────────────────────────────────────────────────────── + + it("does NOT evict the cache entry when the method throws", async () => { + // Pre-populate an entry that must survive the throw + await service.set("safe-key", "keep-me"); + + class FailingService { + @CacheEvict("safe-key") + async doWork(): Promise { + throw new Error("operation failed"); + } + } + + await expect(new FailingService().doWork()).rejects.toThrow("operation failed"); + + // The cache entry must be intact because eviction is skipped on failure + expect(await service.get("safe-key")).toBe("keep-me"); + }); + + // ── Key template interpolation ──────────────────────────────────────────── + + it('resolves "user:{0}" with arg "42" and evicts exactly "user:42"', async () => { + // Populate two keys to confirm only the matching one is removed + await service.set("user:42", { name: "Alice" }); + await service.set("user:99", { name: "Bob" }); + + class UserService { + @CacheEvict("user:{0}") + async deleteUser(id: string) { + return { deleted: id }; + } + } + + await new UserService().deleteUser("42"); + + // "user:42" evicted, "user:99" untouched + expect(await service.get("user:42")).toBeNull(); + expect(await service.get("user:99")).toEqual({ name: "Bob" }); + }); + + // ── Sync method support ─────────────────────────────────────────────────── + + it("works on synchronous methods", async () => { + await service.set("sync-key", "cached"); + + class SyncService { + @CacheEvict("sync-key") + doSync() { + return "done"; + } + } + + const result = await new SyncService().doSync(); + + expect(result).toBe("done"); + expect(await service.get("sync-key")).toBeNull(); + }); + + // ── After eviction, re-population works ────────────────────────────────── + + it("allows re-population of the evicted key on the next set()", async () => { + await service.set("item:1", "old-value"); + + class ItemService { + @CacheEvict("item:1") + async update() { + return "updated"; + } + } + + await new ItemService().update(); + // Evicted — now re-populate + await service.set("item:1", "new-value"); + + expect(await service.get("item:1")).toBe("new-value"); + }); +}); diff --git a/src/decorators/cacheable.decorator.spec.ts b/src/decorators/cacheable.decorator.spec.ts new file mode 100644 index 0000000..6e8599d --- /dev/null +++ b/src/decorators/cacheable.decorator.spec.ts @@ -0,0 +1,175 @@ +/** + * @file cacheable.decorator.spec.ts + * + * Unit tests for the @Cacheable method decorator. + * + * The decorator resolves CacheService via CacheServiceRef at runtime. + * Tests use a real InMemoryCacheStore wired into a real CacheService so + * the full stack is exercised without spinning up a NestJS app. + * + * Tests cover: + * - Cache hit: original method NOT called on second invocation + * - Cache miss: original method called and result persisted on first call + * - Key template interpolation: "user:{0}" with arg "42" → "user:42" + * - Optional TTL forwarded to the store + * - Works on async methods + * - Works on sync methods + */ + +import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; +import { CacheServiceRef } from "@utils/cache-service-ref"; + +import type { CacheModuleOptions } from "../cache-kit.module"; +import { CacheService } from "../services/cache.service"; +import { Cacheable } from "./cacheable.decorator"; + +// --------------------------------------------------------------------------- +// Setup helpers +// --------------------------------------------------------------------------- + +/** Wire a real CacheService into CacheServiceRef so decorators work */ +function setupCacheServiceRef(ttl?: number): CacheService { + const store = new InMemoryCacheStore(); + const options: CacheModuleOptions = + ttl !== undefined ? { store: "memory", ttl } : { store: "memory" }; + const service = new (CacheService as new ( + store: InMemoryCacheStore, + options: CacheModuleOptions, + ) => CacheService)(store, options); + CacheServiceRef.set(service); + return service; +} + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("@Cacheable decorator", () => { + let cacheService: CacheService; + + beforeEach(() => { + // Fresh CacheService (and backing store) before every test + cacheService = setupCacheServiceRef(); + }); + + // ── Cache miss then hit ─────────────────────────────────────────────────── + + it("calls the underlying method on the first call (cache miss)", async () => { + const impl = jest.fn().mockResolvedValue({ id: 1 }); + + class UserService { + @Cacheable("user:1") + async findUser() { + return impl(); + } + } + + const svc = new UserService(); + const result = await svc.findUser(); + + // impl must have been called exactly once + expect(impl).toHaveBeenCalledTimes(1); + expect(result).toEqual({ id: 1 }); + }); + + it("returns the cached value and does NOT call the method on subsequent calls", async () => { + const impl = jest.fn().mockResolvedValue({ id: 1 }); + + class UserService { + @Cacheable("user:static") + async findUser() { + return impl(); + } + } + + const svc = new UserService(); + await svc.findUser(); // miss — populates cache + const result = await svc.findUser(); // hit — must NOT call impl again + + expect(impl).toHaveBeenCalledTimes(1); + expect(result).toEqual({ id: 1 }); + }); + + // ── Key template interpolation ──────────────────────────────────────────── + + it('resolves "user:{0}" with argument "42" to cache key "user:42"', async () => { + const impl = jest.fn().mockResolvedValue({ name: "Alice" }); + + class UserService { + @Cacheable("user:{0}") + async findById(id: string) { + return impl(id); + } + } + + const svc = new UserService(); + await svc.findById("42"); + // Second call with the same argument hits the cached entry + const result = await svc.findById("42"); + + expect(impl).toHaveBeenCalledTimes(1); + expect(result).toEqual({ name: "Alice" }); + + // Different argument → different cache key → another miss + await svc.findById("99"); + expect(impl).toHaveBeenCalledTimes(2); + }); + + it("stores different results for different argument values", async () => { + const impl = jest.fn().mockImplementation(async (id: string) => ({ id })); + + class UserService { + @Cacheable("user:{0}") + async findById(id: string) { + return impl(id); + } + } + + const svc = new UserService(); + const r1 = await svc.findById("1"); + const r2 = await svc.findById("2"); + + expect(r1).toEqual({ id: "1" }); + expect(r2).toEqual({ id: "2" }); + expect(impl).toHaveBeenCalledTimes(2); + }); + + // ── TTL forwarding ──────────────────────────────────────────────────────── + + it("forwards the TTL to CacheService.set()", async () => { + const setSpy = jest.spyOn(cacheService, "set"); + const impl = jest.fn().mockResolvedValue("data"); + + class DataService { + @Cacheable("data-key", 60) + async fetch() { + return impl(); + } + } + + await new DataService().fetch(); + // The explicit 60-second TTL must have been passed to set() + expect(setSpy).toHaveBeenCalledWith("data-key", "data", 60); + }); + + // ── Sync method support ─────────────────────────────────────────────────── + + it("works on synchronous methods", async () => { + const impl = jest.fn().mockReturnValue(42); + + class CalcService { + @Cacheable("sync-key") + compute() { + return impl(); + } + } + + const svc = new CalcService(); + const r1 = await svc.compute(); + const r2 = await svc.compute(); + + expect(impl).toHaveBeenCalledTimes(1); + expect(r1).toBe(42); + expect(r2).toBe(42); + }); +}); diff --git a/src/services/cache.service.spec.ts b/src/services/cache.service.spec.ts new file mode 100644 index 0000000..c3e8df8 --- /dev/null +++ b/src/services/cache.service.spec.ts @@ -0,0 +1,203 @@ +/** + * @file cache.service.spec.ts + * + * Unit tests for CacheService — the primary public API for caching. + * + * Uses an InMemoryCacheStore as the backing adapter instead of mocking the + * ICacheStore interface so that full integration through the real store is + * exercised without requiring a Redis server. + * + * Tests cover: + * - get / set / delete / clear delegating correctly to the store + * - TTL resolution: per-call TTL overrides module default; no TTL falls back to default + * - has(): returns true when a live entry exists, false otherwise + * - wrap(): calls fn once on miss, returns cached value on subsequent calls + */ + +import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; +import type { ICacheStore } from "@ports/cache-store.port"; + +import type { CacheModuleOptions } from "../cache-kit.module"; +import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "../constants"; +import { CacheService } from "./cache.service"; + +// --------------------------------------------------------------------------- +// Factory helpers +// --------------------------------------------------------------------------- + +/** Build a CacheService wired with the provided store and options */ +function buildService(store: ICacheStore, options: Partial = {}): CacheService { + // Manually construct with the two required inject tokens + // (avoids spinning up a full NestJS testing module for pure unit tests) + return new (CacheService as new ( + store: ICacheStore, + options: CacheModuleOptions, + ) => CacheService)( + store, + // Merge supplied options with a safe default (memory store, no default TTL) + { store: "memory", ...options }, + ); +} + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("CacheService", () => { + let store: InMemoryCacheStore; + let service: CacheService; + + beforeEach(() => { + // Use a fresh InMemoryCacheStore so tests are isolated from one another + store = new InMemoryCacheStore(); + service = buildService(store); + }); + + // ── get ────────────────────────────────────────────────────────────────── + + describe("get()", () => { + it("returns null when the key does not exist", async () => { + expect(await service.get("absent")).toBeNull(); + }); + + it("returns the stored value when the key exists", async () => { + await store.set("key", { x: 1 }); + expect(await service.get("key")).toEqual({ x: 1 }); + }); + }); + + // ── set ────────────────────────────────────────────────────────────────── + + describe("set()", () => { + it("stores the value and it is retrievable via get()", async () => { + await service.set("key", "hello"); + expect(await service.get("key")).toBe("hello"); + }); + + it("uses per-call TTL when provided", async () => { + // Spy on the store to verify the exact TTL argument forwarded + const spy = jest.spyOn(store, "set"); + await service.set("key", "v", 60); + expect(spy).toHaveBeenCalledWith("key", "v", 60); + }); + + it("falls back to module default TTL when no per-call TTL is given", async () => { + // Build a service with a default TTL of 30 s + const svcWithDefault = buildService(store, { ttl: 30 }); + const spy = jest.spyOn(store, "set"); + await svcWithDefault.set("key", "v"); + expect(spy).toHaveBeenCalledWith("key", "v", 30); + }); + + it("passes undefined TTL when neither per-call nor module default is set", async () => { + const spy = jest.spyOn(store, "set"); + await service.set("key", "v"); // service has no default TTL + expect(spy).toHaveBeenCalledWith("key", "v", undefined); + }); + }); + + // ── delete ─────────────────────────────────────────────────────────────── + + describe("delete()", () => { + it("removes an existing entry", async () => { + await service.set("key", "value"); + await service.delete("key"); + expect(await service.get("key")).toBeNull(); + }); + + it("does not throw when deleting a non-existent key", async () => { + await expect(service.delete("ghost")).resolves.toBeUndefined(); + }); + }); + + // ── clear ───────────────────────────────────────────────────────────────── + + describe("clear()", () => { + it("removes all entries from the store", async () => { + await service.set("a", 1); + await service.set("b", 2); + await service.clear(); + expect(await service.get("a")).toBeNull(); + expect(await service.get("b")).toBeNull(); + }); + }); + + // ── has ────────────────────────────────────────────────────────────────── + + describe("has()", () => { + it("returns true when a live entry exists", async () => { + await service.set("key", "value"); + expect(await service.has("key")).toBe(true); + }); + + it("returns false when the key does not exist", async () => { + expect(await service.has("missing")).toBe(false); + }); + + it("returns false after the entry has been deleted", async () => { + await service.set("key", "value"); + await service.delete("key"); + expect(await service.has("key")).toBe(false); + }); + }); + + // ── wrap ───────────────────────────────────────────────────────────────── + + describe("wrap()", () => { + it("calls fn and caches the result on the first call (cache miss)", async () => { + const fn = jest.fn().mockResolvedValue({ id: 1 }); + + const result = await service.wrap("key", fn); + + // fn must have been invoked exactly once + expect(fn).toHaveBeenCalledTimes(1); + expect(result).toEqual({ id: 1 }); + }); + + it("returns the cached value without calling fn on subsequent calls", async () => { + const fn = jest.fn().mockResolvedValue({ id: 1 }); + + // First call populates the cache + await service.wrap("key", fn); + // Second call must be a cache hit — fn should NOT be called again + const result = await service.wrap("key", fn); + + expect(fn).toHaveBeenCalledTimes(1); + expect(result).toEqual({ id: 1 }); + }); + + it("uses the per-call TTL when provided", async () => { + const spy = jest.spyOn(store, "set"); + await service.wrap("key", async () => "val", 45); + expect(spy).toHaveBeenCalledWith("key", "val", 45); + }); + + it("falls back to the module default TTL when no TTL is passed to wrap()", async () => { + const svcWithDefault = buildService(store, { ttl: 120 }); + const spy = jest.spyOn(store, "set"); + await svcWithDefault.wrap("key", async () => "val"); + expect(spy).toHaveBeenCalledWith("key", "val", 120); + }); + + it("propagates errors thrown by fn without caching anything", async () => { + const fn = jest.fn().mockRejectedValue(new Error("db failure")); + + await expect(service.wrap("key", fn)).rejects.toThrow("db failure"); + // The key must not have been cached + expect(await service.get("key")).toBeNull(); + }); + }); + + // ── DI token constants (smoke) ──────────────────────────────────────────── + + describe("DI token constants", () => { + it("CACHE_STORE token equals the expected string", () => { + // Guards against accidental token renames + expect(CACHE_STORE).toBe("CACHE_STORE"); + }); + + it("CACHE_MODULE_OPTIONS token equals the expected string", () => { + expect(CACHE_MODULE_OPTIONS).toBe("CACHE_MODULE_OPTIONS"); + }); + }); +}); diff --git a/src/utils/cache-service-ref.spec.ts b/src/utils/cache-service-ref.spec.ts new file mode 100644 index 0000000..1f77c3d --- /dev/null +++ b/src/utils/cache-service-ref.spec.ts @@ -0,0 +1,71 @@ +/** + * @file cache-service-ref.spec.ts + * + * Unit tests for the CacheServiceRef singleton accessor. + * + * Tests cover: + * - get() throws with a descriptive message when called before set() + * - set() stores the instance and get() returns it + * - set() can overwrite an existing instance (hot-reload safety) + */ + +import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; + +import type { CacheModuleOptions } from "../cache-kit.module"; +import { CacheService } from "../services/cache.service"; +import { CacheServiceRef } from "./cache-service-ref"; + +// --------------------------------------------------------------------------- +// Helper to build a minimal CacheService instance +// --------------------------------------------------------------------------- + +function makeCacheService(): CacheService { + const store = new InMemoryCacheStore(); + const options: CacheModuleOptions = { store: "memory" }; + return new (CacheService as new ( + store: InMemoryCacheStore, + options: CacheModuleOptions, + ) => CacheService)(store, options); +} + +// --------------------------------------------------------------------------- +// Test suite +// --------------------------------------------------------------------------- + +describe("CacheServiceRef", () => { + // Reset the singleton before each test by setting it back to null via set() + // We cannot set it to null directly from outside, so we reset after each test + // by storing and restoring the internal _instance via a fresh CacheService. + + it("throws a descriptive error when get() is called before set()", () => { + // Forcibly clear the internal instance by overwriting the module state + // We rely on Jest module isolation — each test file gets its own module instance + // The simplest approach: import the raw module and reset the private variable + // via the accessor itself using an indirect reset. + // Since we cannot set null directly, we rely on a fresh jest module reset. + jest.resetModules(); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { CacheServiceRef: fresh } = + require("./cache-service-ref") as typeof import("./cache-service-ref"); + + // get() on an uninitialised ref must throw with a helpful message + expect(() => fresh.get()).toThrow(/CacheService is not initialised/); + }); + + it("returns the stored instance after set() is called", () => { + const service = makeCacheService(); + CacheServiceRef.set(service); + expect(CacheServiceRef.get()).toBe(service); + }); + + it("allows overwriting the instance (safe for hot-reload)", () => { + const first = makeCacheService(); + const second = makeCacheService(); + + CacheServiceRef.set(first); + CacheServiceRef.set(second); + + // get() must return the most recently set instance + expect(CacheServiceRef.get()).toBe(second); + }); +}); diff --git a/src/utils/resolve-cache-key.util.spec.ts b/src/utils/resolve-cache-key.util.spec.ts new file mode 100644 index 0000000..858e95e --- /dev/null +++ b/src/utils/resolve-cache-key.util.spec.ts @@ -0,0 +1,91 @@ +/** + * @file resolve-cache-key.util.spec.ts + * + * Unit tests for the resolveCacheKey() utility function. + * + * Tests cover: + * - Static keys (no placeholders) returned unchanged + * - Single-argument interpolation: {0} → args[0] + * - Multi-argument interpolation: {0}, {1}, {2} + * - "user:{0}" with argument 42 produces "user:42" (acceptance-criteria example) + * - Missing argument → empty string substitution + * - Non-string argument types (number, boolean, object) are coerced via String() + */ + +import { resolveCacheKey } from "./resolve-cache-key.util"; + +describe("resolveCacheKey()", () => { + // ── Static keys ─────────────────────────────────────────────────────────── + + it("returns the template unchanged when no placeholders are present", () => { + // A static key must pass through as-is + expect(resolveCacheKey("all-products", [])).toBe("all-products"); + }); + + it("returns the template unchanged when args array is empty but key is static", () => { + expect(resolveCacheKey("config:global", [])).toBe("config:global"); + }); + + // ── Single argument ─────────────────────────────────────────────────────── + + it('resolves "user:{0}" with arg "42" to "user:42" (acceptance-criteria)', () => { + // This is the exact example from the task description + expect(resolveCacheKey("user:{0}", ["42"])).toBe("user:42"); + }); + + it("substitutes a numeric argument by coercing it with String()", () => { + expect(resolveCacheKey("user:{0}", [42])).toBe("user:42"); + }); + + it("substitutes a boolean argument correctly", () => { + expect(resolveCacheKey("flag:{0}", [true])).toBe("flag:true"); + }); + + // ── Multi-argument ──────────────────────────────────────────────────────── + + it("substitutes multiple placeholders in order", () => { + expect(resolveCacheKey("post:{0}:comment:{1}", ["5", "99"])).toBe("post:5:comment:99"); + }); + + it("handles three arguments", () => { + expect(resolveCacheKey("{0}:{1}:{2}", ["a", "b", "c"])).toBe("a:b:c"); + }); + + it("uses each argument only for its own index (no cross-substitution)", () => { + // {1} must not be replaced by args[0] + expect(resolveCacheKey("x:{1}", ["first", "second"])).toBe("x:second"); + }); + + // ── Missing arguments ───────────────────────────────────────────────────── + + it("replaces a missing argument placeholder with an empty string", () => { + // {0} present but args is empty → becomes "" + expect(resolveCacheKey("key:{0}", [])).toBe("key:"); + }); + + it("replaces a placeholder for an out-of-range index with an empty string", () => { + // args[0] exists but {1} is out of range + expect(resolveCacheKey("{0}:{1}", ["only-one"])).toBe("only-one:"); + }); + + // ── Null / undefined arguments ──────────────────────────────────────────── + + it("replaces null argument with an empty string", () => { + expect(resolveCacheKey("key:{0}", [null])).toBe("key:"); + }); + + it("replaces undefined argument with an empty string", () => { + expect(resolveCacheKey("key:{0}", [undefined])).toBe("key:"); + }); + + // ── Non-trivial key shapes ──────────────────────────────────────────────── + + it("handles a repeated placeholder (same index used twice)", () => { + // Both {0} occurrences must be substituted + expect(resolveCacheKey("{0}-{0}", ["id"])).toBe("id-id"); + }); + + it("handles a key with no colons (flat namespace)", () => { + expect(resolveCacheKey("item{0}", ["7"])).toBe("item7"); + }); +}); From a6267b30f102a58592cc2cde5347796b1f4230ea Mon Sep 17 00:00:00 2001 From: yasser Date: Fri, 3 Apr 2026 12:30:42 +0100 Subject: [PATCH 8/8] fix(lint): fix import/order and no-require-imports violations in spec files --- src/cache-kit.module.spec.ts | 1 - src/decorators/cache-evict.decorator.spec.ts | 1 + src/decorators/cacheable.decorator.spec.ts | 1 + src/services/cache.service.spec.ts | 1 + src/utils/cache-service-ref.spec.ts | 7 ++++--- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cache-kit.module.spec.ts b/src/cache-kit.module.spec.ts index 7c42b92..069d776 100644 --- a/src/cache-kit.module.spec.ts +++ b/src/cache-kit.module.spec.ts @@ -12,7 +12,6 @@ */ import { Test } from "@nestjs/testing"; - import { CacheServiceRef } from "@utils/cache-service-ref"; import { CacheModule } from "./cache-kit.module"; diff --git a/src/decorators/cache-evict.decorator.spec.ts b/src/decorators/cache-evict.decorator.spec.ts index 27006a2..7edc7b0 100644 --- a/src/decorators/cache-evict.decorator.spec.ts +++ b/src/decorators/cache-evict.decorator.spec.ts @@ -20,6 +20,7 @@ import { CacheServiceRef } from "@utils/cache-service-ref"; import type { CacheModuleOptions } from "../cache-kit.module"; import { CacheService } from "../services/cache.service"; + import { CacheEvict } from "./cache-evict.decorator"; // --------------------------------------------------------------------------- diff --git a/src/decorators/cacheable.decorator.spec.ts b/src/decorators/cacheable.decorator.spec.ts index 6e8599d..05fecaf 100644 --- a/src/decorators/cacheable.decorator.spec.ts +++ b/src/decorators/cacheable.decorator.spec.ts @@ -21,6 +21,7 @@ import { CacheServiceRef } from "@utils/cache-service-ref"; import type { CacheModuleOptions } from "../cache-kit.module"; import { CacheService } from "../services/cache.service"; + import { Cacheable } from "./cacheable.decorator"; // --------------------------------------------------------------------------- diff --git a/src/services/cache.service.spec.ts b/src/services/cache.service.spec.ts index c3e8df8..34f65b9 100644 --- a/src/services/cache.service.spec.ts +++ b/src/services/cache.service.spec.ts @@ -19,6 +19,7 @@ import type { ICacheStore } from "@ports/cache-store.port"; import type { CacheModuleOptions } from "../cache-kit.module"; import { CACHE_MODULE_OPTIONS, CACHE_STORE } from "../constants"; + import { CacheService } from "./cache.service"; // --------------------------------------------------------------------------- diff --git a/src/utils/cache-service-ref.spec.ts b/src/utils/cache-service-ref.spec.ts index 1f77c3d..8a46e61 100644 --- a/src/utils/cache-service-ref.spec.ts +++ b/src/utils/cache-service-ref.spec.ts @@ -13,6 +13,7 @@ import { InMemoryCacheStore } from "@adapters/in-memory-cache-store.adapter"; import type { CacheModuleOptions } from "../cache-kit.module"; import { CacheService } from "../services/cache.service"; + import { CacheServiceRef } from "./cache-service-ref"; // --------------------------------------------------------------------------- @@ -44,9 +45,9 @@ describe("CacheServiceRef", () => { // via the accessor itself using an indirect reset. // Since we cannot set null directly, we rely on a fresh jest module reset. jest.resetModules(); - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { CacheServiceRef: fresh } = - require("./cache-service-ref") as typeof import("./cache-service-ref"); + const { CacheServiceRef: fresh } = jest.requireActual<{ + CacheServiceRef: typeof CacheServiceRef; + }>("./cache-service-ref"); // get() on an uninitialised ref must throw with a helpful message expect(() => fresh.get()).toThrow(/CacheService is not initialised/);