diff --git a/.changeset/big-chicken-poke.md b/.changeset/big-chicken-poke.md new file mode 100644 index 000000000..44dd3329c --- /dev/null +++ b/.changeset/big-chicken-poke.md @@ -0,0 +1,5 @@ +--- +'@gitbook/cache-do': minor +--- + +Experiment with optimizing billable duration in Cloudflare by using multiple RPC sessions instead of one diff --git a/packages/cache-do/src/CacheObjectStub.ts b/packages/cache-do/src/CacheObjectStub.ts index 54203c642..2c498e3a8 100644 --- a/packages/cache-do/src/CacheObjectStub.ts +++ b/packages/cache-do/src/CacheObjectStub.ts @@ -22,7 +22,7 @@ const doLocationHints: { * Client to access a cache tag. */ export class CacheObjectStub { - private opened: CacheObjectDescriptor | null = null; + private stub: DurableObjectStub; constructor( /** Binding to the CacheObject durable object */ @@ -31,41 +31,42 @@ export class CacheObjectStub { private locationId: CacheLocationId, /** Name of the tag */ private tag: string, - ) {} + ) { + const groupId = getCacheObjectIdName(this.locationId, this.tag); + this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), { + // Initialize the object with a locaiton hint, + // as we might want to purge all locations before the object is created. + // https://developers.cloudflare.com/durable-objects/reference/data-location/ + locationHint: doLocationHints[this.locationId], + }); + } /** - * Open the cache object. + * Open a descriptor to the cache object. + * It can be used to perform multiple operations in a single RPC session. + * Ex: + * ```ts + * using desc = cache.open(); + * await desc.set('key', 'value', Date.now() + 1000); + * await desc.get('key'); + * ``` */ async open() { - if (!this.opened) { - const groupId = getCacheObjectIdName(this.locationId, this.tag); - const cacheGroup = this.doNamespace.get(this.doNamespace.idFromName(groupId), { - // Initialize the object with a locaiton hint, - // as we might want to purge all locations before the object is created. - // https://developers.cloudflare.com/durable-objects/reference/data-location/ - locationHint: doLocationHints[this.locationId], - }); - this.opened = await cacheGroup.open(); - } - - return this.opened; + return await this.stub.open(); } /** * Get a value from the cache. */ async get(key: string) { - const desc = await this.open(); - return await desc.get(key); + return (await this.stub.get(key)) as Value | undefined; } /** * Set a value in the cache. */ async set(key: string, value: Value, expiresAt: number) { - // TODO: Should we write on all locations instead of just the current one? - const desc = await this.open(); - return await desc.set(key, value, expiresAt); + return await this.stub.set(key, value, expiresAt); } /** diff --git a/packages/gitbook/src/lib/cache/cloudflare-kv.ts b/packages/gitbook/src/lib/cache/cloudflare-kv.ts index dd85ee2db..5ce5d3381 100644 --- a/packages/gitbook/src/lib/cache/cloudflare-kv.ts +++ b/packages/gitbook/src/lib/cache/cloudflare-kv.ts @@ -15,6 +15,7 @@ interface KVTagMetadata { */ const noKVTags = new Set([ // docs.gitbook.com + 'url:docs.gitbook.com', 'site:site_p4Xo4', 'space:NkEGS7hzeqa35sMXQZ4X', ]);