Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 8, 2024
1 parent d668f18 commit 0e76cd6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/bentocache/src/bento_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export class BentoCache<KnownCaches extends Record<string, BentoStore>> implemen
/**
* Get a value from the cache
*/
get<T = any>(options: GetPojoOptions<T>): Promise<T>
get<T = any>(key: string): Promise<T | null | undefined>
get<T = any>(key: string, defaultValue: Factory<T>, options?: GetOptions): Promise<T>
async get<T = any>(
keyOrOptions: string | GetPojoOptions<T>,
defaultValue?: Factory<T>,
Expand Down
6 changes: 6 additions & 0 deletions packages/bentocache/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class Cache implements CacheProvider {
return new Cache(this.name, this.#stack.namespace(namespace))
}

get<T = any>(options: GetPojoOptions<T>): Promise<T>
get<T = any>(key: string): Promise<T | null | undefined>
get<T = any>(key: string, defaultValue: Factory<T>, options?: GetOptions): Promise<T>
async get<T = any>(
keyOrOptions: string | GetPojoOptions<T>,
defaultValue?: Factory<T>,
Expand Down Expand Up @@ -100,6 +103,9 @@ export class Cache implements CacheProvider {
this.#stack.emit(new events.CacheHit(key, localItem.serialize(), this.name, true))
return localItem.getValue()
}

this.#stack.emit(new events.CacheMiss(key, this.name))
return this.#resolveDefaultValue(defaultValueFn)
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/bentocache/src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface CacheProvider {
*/
get<T = any>(options: GetPojoOptions<T>): Promise<T>
get<T = any>(key: string, defaultValue?: Factory<T>, options?: GetOptions): Promise<T>
get<T = any>(key: string): Promise<T | null | undefined>

/**
* Get or set a value in the cache
Expand Down
14 changes: 14 additions & 0 deletions packages/bentocache/tests/cache/one_tier_local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ test.group('One tier tests', () => {
assert.isUndefined(r2)
})

test('get() with grace period and default value but no fallback value', async ({ assert }) => {
const { cache } = new CacheFactory()
.withMemoryL1()
.merge({ gracePeriod: { enabled: true, duration: '4h' } })
.create()

const result = await cache.get({
key: 'key',
defaultValue: 'default',
})

assert.equal(result, 'default')
})

test('get() should not use grace period when disabled', async ({ assert }) => {
const { cache } = new CacheFactory()
.withMemoryL1()
Expand Down
12 changes: 12 additions & 0 deletions packages/bentocache/tests/typings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ test.group('Typings', () => {
const r1 = await cache.getOrSet<string>('key', () => 'hey')
const r2 = await cache.getOrSet('key', () => 32)
const r3 = await cache.getOrSet('key', () => 50_000)
const r4 = await cache.getOrSet({
key: 'key',
ttl: 1000,
factory: () => 34,
})

expectTypeOf(r1).toEqualTypeOf<string>()
expectTypeOf(r2).toEqualTypeOf<number>()
expectTypeOf(r3).toEqualTypeOf<number>()
expectTypeOf(r4).toEqualTypeOf<number>()
})

test('getOrSet() typings on bento', async ({ expectTypeOf }) => {
Expand All @@ -102,10 +108,16 @@ test.group('Typings', () => {
const r1 = await bento.getOrSet<string>('key', () => 'hey')
const r2 = await bento.getOrSet('key', () => 32)
const r3 = await bento.getOrSet('key', () => 50_000)
const r4 = await bento.getOrSet({
key: 'key',
ttl: 1000,
factory: () => 34,
})

expectTypeOf(r1).toEqualTypeOf<string>()
expectTypeOf(r2).toEqualTypeOf<number>()
expectTypeOf(r3).toEqualTypeOf<number>()
expectTypeOf(r4).toEqualTypeOf<number>()
})

test('on() events list', async ({ expectTypeOf }) => {
Expand Down

0 comments on commit 0e76cd6

Please sign in to comment.