@backstage/backend-defaults
Version:
Backend defaults used by Backstage backend apps
51 lines (47 loc) • 1.38 kB
JavaScript
;
var crypto = require('crypto');
var types = require('./types.cjs.js');
class DefaultCacheClient {
#client;
#clientFactory;
#options;
constructor(client, clientFactory, options) {
this.#client = client;
this.#clientFactory = clientFactory;
this.#options = options;
}
async get(key) {
const k = this.getNormalizedKey(key);
const value = await this.#client.get(k);
return value;
}
async set(key, value, opts = {}) {
const k = this.getNormalizedKey(key);
const ttl = opts.ttl !== void 0 ? types.ttlToMilliseconds(opts.ttl) : void 0;
await this.#client.set(k, value, ttl);
}
async delete(key) {
const k = this.getNormalizedKey(key);
await this.#client.delete(k);
}
withOptions(options) {
const newOptions = { ...this.#options, ...options };
return new DefaultCacheClient(
this.#clientFactory(newOptions),
this.#clientFactory,
newOptions
);
}
/**
* Ensures keys are well-formed for any/all cache stores.
*/
getNormalizedKey(candidateKey) {
const wellFormedKey = Buffer.from(candidateKey).toString("base64");
if (wellFormedKey.length < 200) {
return wellFormedKey;
}
return crypto.createHash("sha256").update(candidateKey).digest("base64");
}
}
exports.DefaultCacheClient = DefaultCacheClient;
//# sourceMappingURL=CacheClient.cjs.js.map