crawlee-storage-extensions
Version:
Package for Apify/Crawlee that allows to store encrypted text values into the Storages
32 lines (31 loc) • 847 B
JavaScript
export class CachedKeyValueStore {
kvstore;
#cache = {};
constructor(kvstore) {
this.kvstore = kvstore;
// class constructor
}
async getValue(key) {
if (!this.#cache[key]) {
const result = await this.kvstore.getValue(key);
if (result !== null) {
this.#cache[key] = result;
}
}
return (this.#cache[key] || null);
}
async setValue(key, value) {
if (this.#cache[key] === value
|| (this.#cache[key] == null && value == null)) {
return;
}
if (value === null) {
await this.kvstore.setValue(key, null);
delete this.#cache[key];
}
else {
await this.kvstore.setValue(key, value);
this.#cache[key] = value;
}
}
}