@graphql-mesh/cache-cfw-kv
Version:
31 lines (30 loc) • 965 B
JavaScript
export default class CFWorkerKVCache {
constructor(config) {
// @ts-expect-error - KV is in globalThis for CFW
this.kvNamespace = globalThis[config.namespace];
if (this.kvNamespace === undefined) {
// We don't use mocks because they increase the bundle size.
config.logger?.warn(`Make sure KV Namespace: ${config.namespace} exists.`);
}
}
async get(key) {
return this.kvNamespace?.get(key, 'json');
}
async getKeysByPrefix(prefix) {
const result = await this.kvNamespace?.list({
prefix,
});
if (!result) {
return [];
}
return result.keys.map(keyEntry => keyEntry.name);
}
async set(key, value, options) {
return this.kvNamespace?.put(key, JSON.stringify(value), {
expirationTtl: options?.ttl,
});
}
async delete(key) {
return this.kvNamespace?.delete(key);
}
}