Skip to content

Commit

Permalink
Merge pull request #33 from archimedes-projects/feature/invalidate-ca…
Browse files Browse the repository at this point in the history
…che-runtime

Invalidate cache in runtime
  • Loading branch information
César Alberca authored Jun 30, 2022
2 parents b425f3b + 0462264 commit 0dbf0e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/arch/src/runner/links/cache-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InvalidationPolicy } from '../../cache/invalidation-policy'
import { Command } from '../../use-case/command'

describe('CacheLink', () => {
it('should use the cache', async () => {
it('should not use the cache when cache manager has not this execution registered', async () => {
const { link, cacheManager, cacheLink } = setup()
when(cacheManager.has(anything(), anything())).thenReturn(false)

Expand All @@ -31,6 +31,28 @@ describe('CacheLink', () => {
verify(link.next(anything())).once()
})

it("should not use the cache when 'invalidateCache' flag is true", async () => {
const { link, cacheManager, cacheLink } = setup()

class MockUseCase extends UseCase<unknown, unknown> {
readonly = true

async internalExecute(): Promise<void> {}
}

const context = Context.create({
useCase: new MockUseCase(),
param: undefined,
executionOptions: { inlineError: false, invalidateCache: true }
})
cacheLink.setNext(instance(link))

await cacheLink.next(context)

verify(link.next(anything())).once()
verify(cacheManager.has(anything(), anything())).never()
})

it('should break the link if it is cached', async () => {
const { link, cacheManager, cacheLink } = setup()
when(cacheManager.has(anything(), anything())).thenReturn(true)
Expand Down
2 changes: 1 addition & 1 deletion packages/arch/src/runner/links/cache-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class CacheLink extends BaseLink {
async next(context: Context): Promise<void> {
const name = context.useCase.constructor.name

if (!this.cacheManager.has(name, [context.param])) {
if (context.executionOptions.invalidateCache || !this.cacheManager.has(name, [context.param])) {
this.nextLink.next(context)
}

Expand Down
1 change: 1 addition & 0 deletions packages/arch/src/use-case/execution-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface ExecutionOptions {
inlineError: boolean
invalidateCache: boolean
}

0 comments on commit 0dbf0e2

Please sign in to comment.