Skip to content

Conversation

@tabkram
Copy link
Owner

@tabkram tabkram commented Mar 28, 2025

  • Added a caching mechanism to optimize function executions.
  • Implemented customizable TTL and cache key resolution.
  • Introduced metadata extraction for better cache context awareness.
  • Ensured errors are not cached to prevent stale failures.
  • Provided a callback for handling cache events with detailed context.

Usage

Basic usage

class ExampleService {
  @cache({ ttl: 5000 }) // Cache result for 5 seconds
  async fetchData(id: number): Promise<string> {
    console.log('Fetching data...'); 
    return `Data for ${id}`;
  }
}

const service = new ExampleService();
service.fetchData(1); // Logs: "Fetching data..."
service.fetchData(1); // Returns cached result, no log

Custom usage

const redisCache: CacheStore = new RedisCacheStore();

class ExampleService {
  @cache({
    ttl: ({ metadata, inputs }) => (inputs[0] === 'fast' ? 5000 : 15000), // Dynamic TTL
    keyGenerator: ({ metadata, inputs }) => `${metadata.methodName}:${JSON.stringify(inputs)}`, // Custom cache key
    cacheManager: redisCache, // Use Redis for caching
    onCacheEvent: ({ isCached, cacheKey }) => {
      console.log(isCached ? `Cache hit: ${cacheKey}` : `Cache miss: ${cacheKey}`);
    }
  })
  async fetchData(param: string): Promise<string> {
    console.log('Fetching fresh data...');
    return `Result for ${param}`;
  }
}

const service = new ExampleService();
service.fetchData('fast'); // Logs: "Fetching fresh data..."
service.fetchData('fast'); // Logs: "Cache hit: fetchData:["fast"]"

@tabkram tabkram self-assigned this Mar 28, 2025
@tabkram tabkram added the deploy deploy a canary version to NPM label Mar 28, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 28, 2025

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements
96.16% (-0.32% 🔻)
401/417
🟢 Branches
87.27% (-0.88% 🔻)
233/267
🟢 Functions
94.74% (-0.41% 🔻)
108/114
🟢 Lines
96.72% (-0.43% 🔻)
383/396
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / cache.decorator.ts
100% 100% 100% 100%
🟢 execution/cache.ts 95.65% 61.54% 100% 95.65%
🟢
... / mapStore.ts
85.71% 100% 75% 84.62%

Test suite run success

86 tests passing in 12 suites.

Report generated by 🧪jest coverage report action from e766f2f

@github-actions
Copy link
Contributor

🚫 Unpublished versions: ∅
Created Version: 3.3.1-canary.65.c9a6e09
📦 Published on NPM: View on NPM

⚠️ Note: This version is temporary and will be unpublished on the next PR update or merge.

@github-actions
Copy link
Contributor

🚫 Unpublished versions: 3.3.1-canary.65.c9a6e09
Created Version: 3.3.1-canary.65.8ad6171
📦 Published on NPM: View on NPM

⚠️ Note: This version is temporary and will be unpublished on the next PR update or merge.

…`, `cacheKey`, etc.)

- Added a caching mechanism to optimize function executions.
- Implemented customizable TTL and cache key resolution.
- Introduced metadata extraction for better cache context awareness.
- Ensured errors are not cached to prevent stale failures.
- Provided a callback for handling cache events with detailed context.
@tabkram tabkram changed the title feat: add cache decorator feat: add @cache decorator with configurable options (cacheManager, cacheKey, etc.) Mar 30, 2025
@github-actions
Copy link
Contributor

🚫 Unpublished versions: 3.3.1-canary.65.8ad6171
Created Version: 3.3.1-canary.65.ace5bbe
📦 Published on NPM: View on NPM

⚠️ Note: This version is temporary and will be unpublished on the next PR update or merge.

@tabkram tabkram merged commit b7deb75 into main Mar 30, 2025
6 of 7 checks passed
@github-actions
Copy link
Contributor

🚫 Unpublished versions: 3.3.1-canary.65.ace5bbe

@tabkram tabkram deleted the feature/cache branch March 30, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deploy deploy a canary version to NPM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants