-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add task caching support #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { TaskResult } from "../TaskResult.js"; | ||
|
|
||
| /** | ||
| * Interface for cache providers used by the CachingExecutionStrategy. | ||
| */ | ||
| export interface ICacheProvider { | ||
| /** | ||
| * Retrieves a cached result by its key. | ||
| * @param key The cache key. | ||
| * @returns The cached TaskResult or undefined if not found. | ||
| */ | ||
| get(key: string): Promise<TaskResult | undefined> | TaskResult | undefined; | ||
|
|
||
| /** | ||
| * Stores a result in the cache. | ||
| * @param key The cache key. | ||
| * @param result The task result to cache. | ||
| * @param ttl Optional time-to-live in milliseconds. | ||
| */ | ||
| set(key: string, result: TaskResult, ttl?: number): Promise<void> | void; | ||
|
|
||
| /** | ||
| * Deletes a cached result by its key. | ||
| * @param key The cache key. | ||
| */ | ||
| delete(key: string): Promise<void> | void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { IExecutionStrategy } from "./IExecutionStrategy.js"; | ||
| import { TaskStep } from "../TaskStep.js"; | ||
| import { TaskResult } from "../TaskResult.js"; | ||
| import { ICacheProvider } from "../contracts/ICacheProvider.js"; | ||
|
|
||
| /** | ||
| * Execution strategy that wraps another strategy and adds caching capabilities. | ||
| */ | ||
| export class CachingExecutionStrategy<TContext> implements IExecutionStrategy<TContext> { | ||
| constructor( | ||
| private readonly innerStrategy: IExecutionStrategy<TContext>, | ||
| private readonly cacheProvider: ICacheProvider | ||
| ) {} | ||
|
|
||
| async execute( | ||
| step: TaskStep<TContext>, | ||
| context: TContext, | ||
| signal?: AbortSignal | ||
| ): Promise<TaskResult> { | ||
| if (!step.cache) { | ||
| return this.innerStrategy.execute(step, context, signal); | ||
| } | ||
|
|
||
| const cacheKey = await step.cache.key(context); | ||
| const cachedResult = await this.cacheProvider.get(cacheKey); | ||
|
|
||
| if (cachedResult) { | ||
| if (step.cache.restore) { | ||
| await step.cache.restore(context, cachedResult); | ||
| } | ||
|
|
||
| return { | ||
| ...cachedResult, | ||
| status: "skipped", | ||
| message: cachedResult.message ? `${cachedResult.message} (cached)` : "Task skipped (cached)", | ||
| }; | ||
| } | ||
|
|
||
| const result = await this.innerStrategy.execute(step, context, signal); | ||
|
|
||
| if (result.status === "success") { | ||
| await this.cacheProvider.set(cacheKey, result, step.cache.ttl); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { ICacheProvider } from "../contracts/ICacheProvider.js"; | ||
| import { TaskResult } from "../TaskResult.js"; | ||
|
|
||
| interface CacheEntry { | ||
| result: TaskResult; | ||
| expiresAt?: number; | ||
| } | ||
|
|
||
| /** | ||
| * A simple in-memory implementation of ICacheProvider. | ||
| */ | ||
| export class MemoryCacheProvider implements ICacheProvider { | ||
| private readonly cache = new Map<string, CacheEntry>(); | ||
|
|
||
| get(key: string): TaskResult | undefined { | ||
| const entry = this.cache.get(key); | ||
| if (!entry) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (entry.expiresAt !== undefined && Date.now() > entry.expiresAt) { | ||
| this.cache.delete(key); | ||
| return undefined; | ||
| } | ||
|
|
||
| return entry.result; | ||
| } | ||
|
|
||
| set(key: string, result: TaskResult, ttl?: number): void { | ||
| const entry: CacheEntry = { | ||
| result, | ||
| }; | ||
|
|
||
| if (ttl !== undefined) { | ||
| entry.expiresAt = Date.now() + ttl; | ||
| } | ||
|
|
||
| this.cache.set(key, entry); | ||
| } | ||
|
|
||
| delete(key: string): void { | ||
| this.cache.delete(key); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Date.now()directly in the logic makes the code harder to test and potentially inconsistent. Consider injecting a clock or using a time provider to improve testability and maintainability.