Skip to content

Commit 9c8636c

Browse files
committed
feat: add ability to disable cache with ttl
1 parent e914689 commit 9c8636c

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

packages/client/src/client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
5555
this.contentRouting = new DelegatedRoutingV1HttpApiClientContentRouting(this)
5656
this.peerRouting = new DelegatedRoutingV1HttpApiClientPeerRouting(this)
5757

58-
const cacheEnabled = typeof globalThis.caches !== 'undefined'
58+
this.cacheTTL = init.cacheTTL ?? defaultValues.cacheTTL
59+
const cacheEnabled = (typeof globalThis.caches !== 'undefined') && (this.cacheTTL > 0)
60+
5961
if (cacheEnabled) {
60-
log('cache enabled')
62+
log('cache enabled with ttl %d', this.cacheTTL)
6163
globalThis.caches.open('delegated-routing-v1-cache').then(cache => {
6264
this.cache = cache
6365
}).catch(() => {
6466
this.cache = undefined
6567
})
6668
}
67-
this.cacheTTL = init.cacheTTL ?? defaultValues.cacheTTL
6869
}
6970

7071
get [contentRoutingSymbol] (): ContentRouting {

packages/client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export interface DelegatedRoutingV1HttpApiClientInit extends FilterOptions {
127127

128128
/**
129129
* How long to cache responses for in ms (default: 5 minutes)
130+
* If 0, caching is disabled
130131
*/
131132
cacheTTL?: number
132133
}

packages/client/test/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('delegated-routing-v1-http-api-client', () => {
2121
let client: DelegatedRoutingV1HttpApiClient
2222

2323
beforeEach(() => {
24-
client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl))
24+
client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl), { cacheTTL: 0 })
2525
})
2626

2727
afterEach(async () => {

packages/client/test/routings.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('libp2p content-routing', () => {
2323
let client: DelegatedRoutingV1HttpApiClient
2424

2525
beforeEach(() => {
26-
client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl))
26+
client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl), { cacheTTL: 0 })
2727
})
2828

2929
afterEach(async () => {

0 commit comments

Comments
 (0)