Skip to content

Commit

Permalink
Merge pull request #24 from archimedes-projects/hotfix/new-cache-refe…
Browse files Browse the repository at this point in the history
…rence

hotfix/new cache reference
  • Loading branch information
achamorro-dev authored Feb 11, 2022
2 parents f959dba + 59e01d5 commit c5ed860
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:packages": "parcel build packages/arch packages/utils",
"watch": "parcel watch packages/arch packages/utils",
"test": "jest",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage",
"test:ci": "jest --ci",
"format": "prettier --write --ignore-path .gitignore .",
Expand Down
19 changes: 19 additions & 0 deletions packages/arch/src/cache/cache-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('CacheManager', () => {
it('should use custom cache', () => {
MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z'))
const cache = mock<Cache>()
when(cache.create()).thenReturn(instance(cache))
when(cache.get(anyString())).thenReturn({ createdAt: 1, returns: 1 })
const { cacheManager } = setup({ cache: instance(cache) })
let count = 0
Expand All @@ -71,6 +72,7 @@ describe('CacheManager', () => {
MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z'))
const mockedDatetime = Datetime.now().toMillis() - 1
const cache = mock<Cache>()
when(cache.create()).thenReturn(instance(cache))
when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 })
const { cacheManager } = setup({ cache: instance(cache), ttl: 0 })
let count = 0
Expand All @@ -89,6 +91,7 @@ describe('CacheManager', () => {
MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z'))
const mockedDatetime = Datetime.now().toMillis() - 1
const cache = mock<Cache>()
when(cache.create()).thenReturn(instance(cache))
when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 })
const { cacheManager } = setup({ cache: instance(cache), ttl: 1 })
let count = 0
Expand All @@ -102,6 +105,22 @@ describe('CacheManager', () => {

verify(cache.delete(anyString())).never()
})

it('should use different caches for different keys', () => {
MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z'))
const mockedDatetime = Datetime.now().toMillis() - 1
const cache = mock<Cache>()
when(cache.create()).thenReturn(instance(cache))
when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 })
const { cacheManager } = setup({ cache: instance(cache) })

const fn = (): string => 'foo'

cacheManager.set('foo', () => fn())
cacheManager.set('bar', () => fn())

verify(cache.create()).twice()
})
})

function setup(cacheOptions?: Partial<CacheOptions>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/arch/src/cache/cache-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CacheManager {

set(cacheKey: CacheKey, fn: (...fnArgs: unknown[]) => unknown, ...args: any[]): unknown {
if (!this.caches.has(cacheKey)) {
this.caches.set(cacheKey, this.cacheOptions.cache)
this.caches.set(cacheKey, this.cacheOptions.cache.create())
}

const existingCache = this.caches.get(cacheKey)!
Expand Down
1 change: 1 addition & 0 deletions packages/arch/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface CacheResult<T = unknown> {
}

export interface Cache<T = unknown> {
create(): Cache<T>
get(key: CacheKey): CacheResult<T> | undefined
set(key: CacheKey, value: CacheResult<T>): void
has(key: CacheKey): boolean
Expand Down
4 changes: 4 additions & 0 deletions packages/arch/src/cache/lru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export class LruCache<T> implements Cache<T> {
has(key: CacheKey) {
return this._lru.has(key)
}

create(): Cache<T> {
return new LruCache()
}
}

0 comments on commit c5ed860

Please sign in to comment.