Skip to content

Commit 2ff7231

Browse files
author
dustin deus
committed
add comment about ttl unit
1 parent f0aa9d9 commit 2ff7231

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/http-data-source.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type AbortSignal = unknown
1616

1717
export type CacheTTLOptions = {
1818
requestCache?: {
19-
// The maximum time an item is cached
19+
// The maximum time an item is cached (seconds)
2020
maxTtl: number
21-
// The maximum time an item fetched from the cache is case of an error. This value must be greater than `maxTtl`.
21+
// The maximum time an item fetched from the cache is case of an error (seconds). This value must be greater than `maxTtl`
2222
maxTtlIfError: number
2323
}
2424
}
@@ -44,6 +44,7 @@ export type Response<TResult> = {
4444
body: TResult
4545
memoized: boolean
4646
isFromCache: boolean
47+
// maximum ttl (seconds)
4748
maxTtl?: number
4849
} & Omit<ResponseData, 'body'>
4950

@@ -77,6 +78,7 @@ function apolloKeyValueCacheToKeyv(cache: KeyValueCache): Store<string> {
7778
return true
7879
},
7980
set(key: string, value: string, ttl?: number) {
81+
// apollo works with seconds
8082
return cache.set(key, value, {
8183
ttl,
8284
})

test/http-data-source.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ test('Response is cached', async (t) => {
611611
getFoo() {
612612
return this.get(path, {
613613
requestCache: {
614-
maxTtl: 100,
615-
maxTtlIfError: 200,
614+
maxTtl: 10,
615+
maxTtlIfError: 20,
616616
},
617617
})
618618
}
@@ -641,13 +641,13 @@ test('Response is cached', async (t) => {
641641
let response = await dataSource.getFoo()
642642
t.false(response.isFromCache)
643643
t.false(response.memoized)
644-
t.is(response.maxTtl, 200)
644+
t.is(response.maxTtl, 20)
645645
t.deepEqual(response.body, { name: 'foo' })
646646

647647
response = await dataSource.getFoo()
648648
t.false(response.isFromCache)
649649
t.true(response.memoized)
650-
t.is(response.maxTtl, 200)
650+
t.is(response.maxTtl, 20)
651651
t.deepEqual(response.body, { name: 'foo' })
652652

653653
dataSource = new (class extends HTTPDataSource {
@@ -657,8 +657,8 @@ test('Response is cached', async (t) => {
657657
getFoo() {
658658
return this.get(path, {
659659
requestCache: {
660-
maxTtl: 100,
661-
maxTtlIfError: 200,
660+
maxTtl: 10,
661+
maxTtlIfError: 20,
662662
},
663663
})
664664
}
@@ -669,7 +669,7 @@ test('Response is cached', async (t) => {
669669
response = await dataSource.getFoo()
670670
t.true(response.isFromCache)
671671
t.false(response.memoized)
672-
t.is(response.maxTtl, 200)
672+
t.is(response.maxTtl, 20)
673673
t.deepEqual(response.body, { name: 'foo' })
674674

675675
const cached = JSON.parse(map.get('keyv:' + baseURL + path)!)
@@ -725,8 +725,8 @@ test('Fallback from cache on origin error', async (t) => {
725725
getFoo() {
726726
return this.get(path, {
727727
requestCache: {
728-
maxTtl: 100,
729-
maxTtlIfError: 200,
728+
maxTtl: 10,
729+
maxTtlIfError: 20,
730730
},
731731
})
732732
}
@@ -755,7 +755,7 @@ test('Fallback from cache on origin error', async (t) => {
755755
let response = await dataSource.getFoo()
756756
t.false(response.isFromCache)
757757
t.false(response.memoized)
758-
t.is(response.maxTtl, 200)
758+
t.is(response.maxTtl, 20)
759759

760760
t.deepEqual(response.body, { name: 'foo' })
761761

@@ -770,8 +770,8 @@ test('Fallback from cache on origin error', async (t) => {
770770
getFoo() {
771771
return this.get(path, {
772772
requestCache: {
773-
maxTtl: 100,
774-
maxTtlIfError: 200,
773+
maxTtl: 10,
774+
maxTtlIfError: 20,
775775
},
776776
})
777777
}
@@ -782,7 +782,7 @@ test('Fallback from cache on origin error', async (t) => {
782782
response = await dataSource.getFoo()
783783
t.true(response.isFromCache)
784784
t.false(response.memoized)
785-
t.is(response.maxTtl, 200)
785+
t.is(response.maxTtl, 20)
786786

787787
t.deepEqual(response.body, { name: 'foo' })
788788

@@ -816,8 +816,8 @@ test('Should not cache POST requests', async (t) => {
816816
postFoo() {
817817
return this.post(path, {
818818
requestCache: {
819-
maxTtl: 100,
820-
maxTtlIfError: 200,
819+
maxTtl: 10,
820+
maxTtlIfError: 20,
821821
},
822822
})
823823
}
@@ -837,7 +837,7 @@ test('Should not cache POST requests', async (t) => {
837837
return map.get(key)
838838
},
839839
async set(key: string, value: string, options: KeyValueCacheSetOptions) {
840-
t.deepEqual(options, { ttl: 100 })
840+
t.deepEqual(options, { ttl: 10 })
841841
map.set(key, value)
842842
},
843843
},
@@ -876,8 +876,8 @@ test('Response is not cached due to origin error', async (t) => {
876876
getFoo() {
877877
return this.get(path, {
878878
requestCache: {
879-
maxTtl: 100,
880-
maxTtlIfError: 200,
879+
maxTtl: 10,
880+
maxTtlIfError: 20,
881881
},
882882
})
883883
}

0 commit comments

Comments
 (0)