@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
41 lines • 1.24 kB
JavaScript
import { LRUCache } from 'least-recent';
import { CodenameContainer } from 'cartonne';
export const IPFS_CACHE_HIT = 'ipfs_cache_hit';
export const IPFS_CACHE_MISS = 'ipfs_cache_miss';
export class IPLDRecordsCache {
constructor(capacity) {
this.cache = new LRUCache(capacity);
this.codecs = new CodenameContainer('codecs');
}
set(cid, entry) {
this.cache.set(cid.toString(), entry);
}
setRecord(cid, record) {
if (this.cache.has(cid.toString()))
return;
const codec = this.codecs.get(cid.code);
this.cache.set(cid.toString(), {
record: record,
block: codec.encode(record),
});
}
setBlock(cid, bytes) {
if (this.cache.has(cid.toString()))
return;
const codec = this.codecs.get(cid.code);
this.cache.set(cid.toString(), {
record: codec.decode(bytes),
block: bytes,
});
}
get(cid) {
return this.cache.get(cid.toString());
}
setWithResolutionPath(query, entry) {
this.cache.set(query, entry);
}
getWithResolutionPath(query) {
return this.cache.get(query);
}
}
//# sourceMappingURL=ipld-records-cache.js.map