mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-18 16:45:11 +00:00
Experiment with lower DO billable duration by using direct RPC methods (#2492)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/cache-do': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Experiment with optimizing billable duration in Cloudflare by using multiple RPC sessions instead of one
|
||||||
@@ -22,7 +22,7 @@ const doLocationHints: {
|
|||||||
* Client to access a cache tag.
|
* Client to access a cache tag.
|
||||||
*/
|
*/
|
||||||
export class CacheObjectStub {
|
export class CacheObjectStub {
|
||||||
private opened: CacheObjectDescriptor | null = null;
|
private stub: DurableObjectStub<CacheObject>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
/** Binding to the CacheObject durable object */
|
/** Binding to the CacheObject durable object */
|
||||||
@@ -31,41 +31,42 @@ export class CacheObjectStub {
|
|||||||
private locationId: CacheLocationId,
|
private locationId: CacheLocationId,
|
||||||
/** Name of the tag */
|
/** Name of the tag */
|
||||||
private tag: string,
|
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() {
|
async open() {
|
||||||
if (!this.opened) {
|
return await this.stub.open();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from the cache.
|
* Get a value from the cache.
|
||||||
*/
|
*/
|
||||||
async get<Value = unknown>(key: string) {
|
async get<Value = unknown>(key: string) {
|
||||||
const desc = await this.open();
|
return (await this.stub.get(key)) as Value | undefined;
|
||||||
return await desc.get<Value>(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a value in the cache.
|
* Set a value in the cache.
|
||||||
*/
|
*/
|
||||||
async set<Value = unknown>(key: string, value: Value, expiresAt: number) {
|
async set<Value = unknown>(key: string, value: Value, expiresAt: number) {
|
||||||
// TODO: Should we write on all locations instead of just the current one?
|
return await this.stub.set(key, value, expiresAt);
|
||||||
const desc = await this.open();
|
|
||||||
return await desc.set<Value>(key, value, expiresAt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ interface KVTagMetadata {
|
|||||||
*/
|
*/
|
||||||
const noKVTags = new Set([
|
const noKVTags = new Set([
|
||||||
// docs.gitbook.com
|
// docs.gitbook.com
|
||||||
|
'url:docs.gitbook.com',
|
||||||
'site:site_p4Xo4',
|
'site:site_p4Xo4',
|
||||||
'space:NkEGS7hzeqa35sMXQZ4X',
|
'space:NkEGS7hzeqa35sMXQZ4X',
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user